matlab录制音频并保存
时间: 2023-10-16 22:18:38 浏览: 239
在 MATLAB 中录制音频并保存,可以使用 `audiorecorder` 和 `audiowrite` 两个函数。
首先,使用 `audiorecorder` 函数创建一个音频录制对象:
```matlab
recObj = audiorecorder(fs, nBits, nChannels);
```
其中,`fs` 表示采样率,`nBits` 表示采样位数,`nChannels` 表示声道数。例如,我们可以设置采样率为 44100 Hz,采样位数为 16 位,声道数为 1:
```matlab
fs = 44100;
nBits = 16;
nChannels = 1;
recObj = audiorecorder(fs, nBits, nChannels);
```
然后,使用 `record` 函数开始录制音频:
```matlab
record(recObj);
```
录制一段时间后,使用 `stop` 函数停止录制:
```matlab
stop(recObj);
```
最后,使用 `getaudiodata` 函数获取录制的音频数据,并使用 `audiowrite` 函数将音频保存到文件中:
```matlab
audioData = getaudiodata(recObj);
filename = 'recorded_audio.wav';
audiowrite(filename, audioData, fs);
```
完整的代码如下:
```matlab
fs = 44100;
nBits = 16;
nChannels = 1;
recObj = audiorecorder(fs, nBits, nChannels);
record(recObj);
% 录制 5 秒钟的音频
pause(5);
stop(recObj);
audioData = getaudiodata(recObj);
filename = 'recorded_audio.wav';
audiowrite(filename, audioData, fs);
```
以上代码将会录制 5 秒钟的音频,并将其保存到文件 `recorded_audio.wav` 中。
阅读全文
相关推荐















