matlab通过调用电脑的录音机录一段音频,分析时域和频域
recObj = audiorecorder;
%录音机对象,指定为 audiorecorder 对象。使用 audiorecorder 函数创建该对象。
disp('Start speaking.')
recordblocking(recObj,5);
disp('End of Recording.');
%从麦克风录制20秒的音频片段
doubleArray = getaudiodata(recObj);
plot(doubleArray);
%以 double 数组形式获取音频信号,并绘制数据。
title('Audio Signal (double)');
fs=length(doubleArray)/5;
%做fft变换,绘制频谱图。
f=(0:length(doubleArray)-1)*fs/length(doubleArray);
y_fft=fft(doubleArray);
P_1 = abs(y_fft).^2/length(doubleArray);
plot(f,abs(P_1 ));
xlabel('Frequency (f)');
ylabel('|P(f)|^2');
Y_fft = fftshift(y_fft);
n=length(doubleArray);
%通过shiftfft将零频分量移动到数组中心,重新排列傅里叶变换 。
fshift = (-n/2:n/2-1)*(fs/n);
P_2 = abs(Y_fft ).^2/n;
plot(fshift,abs(P_2));
xlabel('Frequency (f)');
ylabel('|P(f)|^2');
recObj = audiorecorder;
disp('Start speaking.')
recordblocking(recObj,5);
disp('End of Recording.');
doubleArray = getaudiodata(recObj);
plot(doubleArray);
title('Audio Signal (double)');
fs=length(doubleArray)/5;
f=(0:length(doubleArray)-1)*fs/length(doubleArray);
y_fft=fft(doubleArray);
P_1 = abs(y_fft).^2/length(doubleArray);
plot(f,abs(P_1 ));
xlabel('Frequency (f)');
ylabel('|P(f)|^2');
Y_fft = fftshift(y_fft);
n=length(doubleArray);
fshift = (-n/2:n/2-1)*(fs/n);
P_2 = abs(Y_fft ).^2/n;
plot(fshift,abs(P_2));
xlabel('Frequency (f)');
ylabel('|P(f)|^2');