所以我寫了這個,基本上是實時更新頻譜圖。爲什麼運行這個MATLAB代碼時記錄器是空的?
function spec(Fs, n_bits, n_channels, update_rate)
%# Initialise default parameters if not supplied
if (~exist('Fs', 'var'))
Fs = 44000;
end
if (~exist('n_bits', 'var'))
n_bits = 16;
end
if (~exist('n_channels', 'var'))
n_channels = 2;
end
if (~exist('update_rate', 'var'))
update_rate = 5;
end
plot_colors = hsv(n_channels);
%# Initialise plots, one above each other in a single figure window
figure;
%# Time Domain plot
hold on
%# Setup the audiorecorder which will acquire data off default soundcard
audio_recorder = audiorecorder(Fs, n_bits, n_channels);
set(audio_recorder, 'TimerFcn', {@audioRecorderTimerCallback, ...
audio_recorder});
set(audio_recorder, 'TimerPeriod', 1/update_rate);
set(audio_recorder, 'BufferLength', 1/update_rate);
%# Start the recorder
record(audio_recorder);
end
function audioRecorderTimerCallback(obj, event, audio_recorder)
Fs = get(obj, 'SampleRate');
num_channels = get(obj, 'NumberOfChannels');
num_bits = get(obj, 'BitsPerSample');
try
if (num_bits == 8)
data_format = 'int8';
elseif (num_bits == 16)
data_format = 'int16';
elseif (num_bits == 32)
data_format = 'double';
else
error('Unsupported sample size of %d bits', num_bits);
end
%# stop the recorder, grab the data, restart the recorder. May miss some data
stop(obj);
data = getaudiodata(obj, data_format);
record(obj);
if (size(data, 2) ~= num_channels)
error('Soundcard does not support acquisition of %d channels', ...
length(num_channels))
end
data_fft = fft(double(data));
specgram(data_fft,512);
catch
%# Stop the recorder and exit
stop(obj)
rethrow(lasterror)
end
drawnow;
end
我總是以空記錄器結束。
我不明白爲什麼會出現這個問題,我確實先錄製了。
有人請幫我 – Rave
此代碼對我來說是非常有幫助的。一個有用的修改:不要在回調中停止並啓動記錄器。我的意思是除去'stop(obj)'和'record(obj)',並簡單地讀取數據,注意數據長度會不斷增加。這將防止我們錯過某些數據的場合。 –
注意到'stop'命令停止記錄到緩衝區並且'record'命令使用一個新的緩衝區,緩衝區將會繼續增長,並伴隨上面給出的修改。 –