how can I plot a signal on GUI axes?

7 Ansichten (letzte 30 Tage)
John Bitzios
John Bitzios am 8 Okt. 2014
Beantwortet: Geoff Hayes am 8 Okt. 2014
How can I plot a signal on the GUI's axes after I have recorded it trough the audiorecorder command?

Antworten (1)

Geoff Hayes
Geoff Hayes am 8 Okt. 2014
John - if you have a timer for your audio recorder, where the timer fires (for example) every one second (as discussed in your previous question how to trigger the microphone), then your function signature for your timer should look something like
function audioTimer(hObject,varargin)
where hObject is the timer object, and the handle to the figure/GUI is extracted from varargin as
hFigure = varargin{2};
handles = guidata(hFigure);
With this information, we can determine the sample rate and get the audio samples as
Fs = get(hObject,'SampleRate');
samples = getaudiodata(hObject);
Now we check to see if we have enough samples to do some processing
% skip if not enough data
if length(samples)<handles.lastSampleAt+1+Fs
return;
end
In the above, we assume that the handles structure has a field called lastSampleAt which tells us what the last sample is that we extracted. It is further assumed that this field has been initialized to zero in the OpeningFnc of your GUI.
If there is enough data, then we continue as
% grab the time domain data from the last sample extracted plus Fs
X = samples(handles.lastSampleAt+1:handles.lastSampleAt+1+Fs);
% do the FFT where the block size is Fs (in this example)
N = Fs;
Y = fft(X,N);
% update the lastSampleAt field
handles.lastSampleAt = handles.lastSampleAt + Fs;
guidata(hFigure,handles);
Now we need to plot the time domain data on axes1 and the frequency domain data on axes2.
% create a time vector and plot the time domain data
t = linspace(0,1,Fs);
stem(handles.axes1,t,X,'b','MarkerSize',2,'MarkerFaceColor','b');
set(handles.axes1,'Color','k','Tag','axes1');
% create a frequency vector and plot the frequency domain data
f = 0:Fs/N:(N-1)*Fs/N;
plot(handles.axes2,f,Y);
set(handles.axes2,'Color','k','Tag','axes2');
The stem function is used to plot the (sampled) time domain data; whereas the usual plot function is used for the frequency domain data. Note the set calls for both axes. I have fond that the tags (sometimes) get cleared, so these statements are just to reset the tag value and set the background of each axes to black (k).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by