问题描述
我正在尝试在Matlab中制作频谱图,这是我的代码:
i am trying to make a spectrogram in matlab,here is my code:
% 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);
问题是,在我讲话时我希望显示频谱图,所以它应该在我讲话时进行更新.当音频来自麦克风时,如何绘制频谱图?所以我应该能够实时查看频谱图
problem is that while i am speaking i want the spectrogram to be shown, so it should be updating as i speak. How can i plot the spectrogram while the audio is coming from microphone? so i should be able to see spectrogram in real time
我也尝试过
% 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次之后)
but it will only display the spectrogram when the while loop fishes (so after running 60 times)
推荐答案
这是一种可能的实现.主要问题是,您忘记在末尾调用 DRAWNOW 每个循环:
Here is one possible implementation. The main problem was that you forget to call DRAWNOW at the end of each loop:
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.')
我只显示每次迭代中的频率分量.如果需要,您应该可以修改它以显示光谱图...
I simply display the frequency components in each iteration. You should be able to modify that to show the spectrogram if you want...
这篇关于在Matlab中编程(如何实时处理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!