MATLAB Answers

Ploting analog input in real time

8 views (last 30 days)
Raldi
Raldi on 7 Dec 2011
Hey evereyone I have the following code
input=analoginput('winsound'); set (input, 'SampleRate', 8000); set (input, 'SamplesPerTrigger', 1000); num=1000; set (input, 'TriggerRepeat', inf); start(input)
while "something" data = getdata(input); flushdata(input); plot("data"); plot("fft data spectrum") stop(input) delete(input); end
The data plot shows fine, but the second plot will only show something for 1 second just after i run the code and then it shows nothing. Why is that happening?

  0 Comments

Sign in to comment.

Answers (2)

Gautam Vallabha
Gautam Vallabha on 8 Dec 2011
It is possible that one of your plots is being overwritten by the other plot. The standard plot command (plot(x,y)) uses the current figure and axes (gcf and gca), the default behavior is that a new plot will replace any existing plots.
You might try explicitly using different axes, e.g.
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
while (...)
plot(ax1, time, amplitude);
...
plot(ax2, freq, power);
end
By the way, for better performance, instead of calling plot each time, consider calling it once (before the while loop), and then setting xdata and ydata, e.g.:
h = plot(ax1, time, amplitude);
...
set(h,'xdata', newTimeData, 'ydata', newAmplitudeData);

  1 Comment

Raldi
Raldi on 8 Dec 2011
but they are on different objects axes1 and axes2, how can this happen?

Sign in to comment.


Walter Roberson
Walter Roberson on 8 Dec 2011
Do not stop() and delete() input within the loop.
Within the loop, add a drawnow() call after the plot() call.

  5 Comments

Show 2 older comments
Raldi
Raldi on 9 Dec 2011
William haha sorry walter it was quite late at the time and i was a bit tired :P.
<--Code-->
function varargout = REAL_TIME(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @REAL_TIME_OpeningFcn, ...
'gui_OutputFcn', @REAL_TIME_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before REAL_TIME is made visible.
function REAL_TIME_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to REAL_TIME (see VARARGIN)
% ________________________________________________________________________
set(handles.Stop,'Visible','off');%Hides "Stop" push button,to make it
% visible just after "Start" push button has been pushed.
set(handles.Define,'Visible','off');%Hides "Define Another Filter" push
% button, to make it visible just after "Stop" push button has been pushed.
% Choose default command line output for REAL_TIME
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes REAL_TIME wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function varargout = REAL_TIME_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in Define.
function Define_Callback(hObject, eventdata, handles)
% hObject handle to Define (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
MAIN %executes the GUI file named "FILTERS", where we define the filter
% type and its features that we are going to apply to the audio signal
% incoming from the microphone.
close REAL_TIME %Close the present GUI interface
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global a b fsx %This are the global variables, used to import data
% from "FILTERS" GUI interface, where:
% (a)is numerator and (b)denominator of the filter transfer function.
% (fsx) is the sample frequency.
in=analoginput('winsound');
addchannel(in,1);%Adds a hardware channel to the analog object "ca".
%in.SampleRate=fsx;%Defines the sample Rate.
set (in, 'SampleRate', fsx);
%in.SamplesPerTrigger=1000;%Defines the sample number per trigger.
set (in, 'SamplesPerTrigger', 1000);
num_mes=1000;%Defines the sample number to be extracted from the "data"
% variable.
%in.TriggerRepeat=Inf;%Specifies the trigger number executions as infinite.
set (in, 'TriggerRepeat', inf);
%in.TriggerType='Immediate';%The trigger occurs immediately after the start
% function is used.
set (ca, 'TriggerType', 'Immediate');
%in.BitsPerSample=32;
start(in)
set(handles.Start,'UserData',1);%initialize the flag variable, used to
while (get(handles.Start,'UserData') ==1)
set(handles.Define,'Visible','off');
set(handles.Stop,'Visible','on');%Makes visible the "stop" push button
%dat=peekdata(in,num_mes);
dat = getdata(in);
%--OR--
%[DATA, TIME] = getdata(in,num_mes) returns the specified number,
%SAMPLES, of data in time-value pairs
flushdata(in);%Remove all logged data associated to "ca".
% _____________________________________________________________________
% ________Plots the audio signal incoming from the microphone__________
axes(handles.axes7)
plot(dat);%plots the signal incoming from the microphone.
title('Incoming Signal from the Microphone');
ylabel('Relative Amplitude');
% ylim([-1 1]);
% _____________________________________________________________________
% ___Plots the FFT of the audio signal incoming from the microphone____
axes(handles.axes8)
Lo=length(dat);
NFFT = 2^nextpow2(Lo); % Next power of 2 from length of dat.
Yo = fft(dat,NFFT)/Lo;
fo = fsx/2*linspace(0,1,NFFT/2+1);
plot(fo,2*abs(Yo(1:NFFT/2+1)))
title('Original Audio Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Relative Amplitude');
% _____________________________________________________________________
% _____________Plots the FFT of the filtered audio signal______________
axes(handles.axes9)
sfiltrada=filter(b, a, dat);
L=length(sfiltrada);
NFFT = 2^nextpow2(L);
Y = fft(sfiltrada,NFFT)/L;
f = fsx/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Filtered Audio Signal Spectrum');
xlabel('Frequency (Hz)');
ylabel('Relative Amplitude');
drawnow
end
stop(in);%Stop the analog input object "in".
delete(in);%Delete the analog input object "ca".
guidata(hObject, handles);
% --- Executes on button press in Stop.
function Stop_Callback(hObject, eventdata, handles)
% hObject handle to Stop (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Start,'UserData',0);%Set the flag with 0 to stop the acquisition.
set(handles.Define,'Visible','on');i
% push button just after "Stop" push button has been clicked.
guidata(hObject, handles);
Walter Roberson
Walter Roberson on 9 Dec 2011
Please see my comment to my answer in http://www.mathworks.com/matlabcentral/answers/22208-show-figure
Raldi
Raldi on 11 Dec 2011
Either i didnt understand you answer well or it doesnt work in my case cause it still didnt show anything on the spectrum analisation axes.

Sign in to comment.

Sign in to answer this question.