我正在尝试用matlab制作一个谱图,下面是我的代码:
% Record your voice for 100 seconds.
recObj = audiorecorder;
disp('Start speaking.')
recordblocking(recObj, 100);
% Store data in double-precision array.
my= getaudiodata(recObj);
figure;
specgram(my,512);问题是,当我说话时,我想要显示频谱图,所以它应该在我说话时更新。当音频从麦克风中传出时,我如何绘制频谱图?所以我应该能够实时看到频谱图
我也试过这个
% Record your voice for 100 seconds.
recObj = audiorecorder;
disp('Start speaking.')
a=0;
figure;
while a<60
recordblocking(recObj, 100);
% Store data in double-precision array.
my= getaudiodata(recObj);
specgram(my,512);
a=a+1;
end但它只会在while循环钓鱼时显示频谱图(因此在运行60次之后)。
发布于 2011-07-15 03:16:16
以下是一种可能的实现。主要问题是您忘记在每个循环结束时调用DRAWNOW:
Fs = 8000; %# sampling frequency in Hz
T = 1; %# length of one interval signal in sec
t = 0:1/Fs:T-1/Fs; %# time vector
nfft = 2^nextpow2(Fs); %# n-point DFT
numUniq = ceil((nfft+1)/2); %# half point
f = (0:numUniq-1)'*Fs/nfft; %'# frequency vector (one sided)
%# prepare plots
figure
hAx(1) = subplot(211);
hLine(1) = line('XData',t, 'YData',nan(size(t)), 'Color','b', 'Parent',hAx(1));
xlabel('Time (s)'), ylabel('Amplitude')
hAx(2) = subplot(212);
hLine(2) = line('XData',f, 'YData',nan(size(f)), 'Color','b', 'Parent',hAx(2));
xlabel('Frequency (Hz)'), ylabel('Magnitude (dB)')
set(hAx, 'Box','on', 'XGrid','on', 'YGrid','on')
%#specgram(sig, nfft, Fs);
%# prepare audio recording
recObj = audiorecorder(Fs,8,1);
%# Record for 10 intervals of 1sec each
disp('Start speaking...')
for i=1:10
recordblocking(recObj, T);
%# get data and compute FFT
sig = getaudiodata(recObj);
fftMag = 20*log10( abs(fft(sig,nfft)) );
%# update plots
set(hLine(1), 'YData',sig)
set(hLine(2), 'YData',fftMag(1:numUniq))
title(hAx(1), num2str(i,'Interval = %d'))
drawnow %# force MATLAB to flush any queued displays
end
disp('Done.')

我只是简单地显示每次迭代中的频率分量。如果你想的话你应该可以修改它来显示光谱图...
发布于 2011-07-13 23:27:26
MATLAB本质上是单线程的。一次只能发生一件事。这使得实时任务变得有些困难。正如您所提到的,在这100秒过去之前,recordblocking不会将控制权交还给您的脚本。关键在于“阻塞”这个词。
解决这个问题的方法是使用callbacks and non-blocking functions。
The audiorecorder properties
然后record method将启动录制并在后台处理,按照指示调用上述函数。
但这应该是你开始学习的起点。
发布于 2011-07-13 23:26:42
最明显的做法是将您的代码放在循环中,以不断更新图形。但请注意,Matlab并不是为这类任务而设计的,所以我不知道您会取得多大的成功。你有没有试过在谷歌上搜索能帮你做到这一点的自由软件?如果没有什么东西不能做到这一点,我会很惊讶。
https://stackoverflow.com/questions/6681063
复制相似问题