三天不学习,就不知道如何用声卡了。
做了一个简单的demo,仅采集1次的声音,并做FFT 分析
记得存为.m文件,不要.mlx文件,因为你不知道何时开始。
% record 只做做了 一次采集,如何做连续采集,大家尝试
% 连续采集,记得设置退出条件,比如捕捉ecs按键
timeLength=2; % 采样时长,单位秒
Fs=44100;%Hz
samples=timeLength*Fs; % 默认采样率44100,计算采样点数
nBits=16;%bit
nChannels=1,
ID=1;% 0 or 1 or -1
recObj = audiorecorder(Fs,nBits,nChannels,ID);
disp('Start speaking.')
recordblocking(recObj, timeLength);
disp('End of Recording.');
% Store data in double-precision array.
myRecording = getaudiodata(recObj);
% Plot the waveform.
plot(myRecording);
% Play back the recording.
play(recObj);
figure %('Name','实时频谱','MenuBar'...
% ,'none','ToolBar','none','NumberTitle','off');
audioIn=myRecording;%data
% xdata=(1:1:samples/2)/timeLength;
% raw audio data
xdata=(1:1:samples)*1/Fs;% 采样时间间隔数据
subplot(1,2,1),plot(xdata,audioIn)
xlabel('Time/s');ylabel('Amplitude/mv');
title('Time domain')
% FFT
ydata_fft=fft(audioIn); % 傅里叶变换
ydata_abs=2*abs(ydata_fft(1:samples/2))';% 取绝对值
f = Fs*(0:(samples/2)-1)/samples;% 频率轴
subplot(1,2,2) %子图2
plot(f,ydata_abs)
xlabel('Frequency/Hz');ylabel('Amplitude/mv');
title('Frequnecy domain')
用matlab获得的声音很怪。labview采集的效果就非常好!