From: Peng Liang on
Hi experts,

I learned how to continuously acquiring data from microphone from the demo of Continuous Acquisition Using Analog Input. Now I am able to plotting data in frequency domain live. I also trying to plotting live data in time domain. what I want is oscilloscope which will display the input signal in live mode both time domain and frequency domain(spectrum). my code is following almost same as the demo.

% This function calculates the fft of the data, updates the plot and
% returns a true if it detects a frequency in the range of 2500-3000 Hz.
function condition = ai_continuous_fft(data,plotHandle)

lengthofData = length(data);
nPower2 = 2 ^ nextpow2(lengthofData);
fs = 8000; % Sample Rate
yDFT = fft(data,nPower2-1); % Discrete Fourier Transform of data
freqRange = (0:nPower2-1)*(fs/nPower2);
gfreq = freqRange(1:floor(nPower2/2)); % Only plotting upto n/2


h = yDFT(1:floor(nPower2/2));
abs_h = abs(h);
threshold = 10;
set(plotHandle, 'ydata', abs_h, 'xdata', gfreq); % updating the plot
drawnow; % update the plot

val = max(abs_h(gfreq>2500 & gfreq<3000)); % checking for the frequency
if (val>threshold)
condition = 1;
else
condition = 0;
end

% This callback function executes each time the time specified by
% TimerPeriod passes. The input parameters obj and event are passed
% implicitly in the callback function.
%
% * obj is the analog input object ai
% * event is a variable that stores the data contained in the EventLog
% property
% This function calls the ai_continuous_fft function to check whether the
% frequency is detected.
% If the frequency is detected then the callback issues a stop command to
% the analog input object.
function ai_continuous_timer_callback(obj,event,plotHandle,titleHandle)

persistent count;
persistent totalData;
if isempty(count)
count = 0;
end
count = count + 1;

% Get only the number of samples that are available
[data,time] = getdata(obj,obj.samplesAvailable);
% First time through assign the data to totalData, else append it.
if isempty(totalData)
totalData.time = time;
totalData.data = data;
else
totalData.time = [totalData.time;time];
totalData.data = [totalData.data;data];
end
% Call ai_continuous_fft to check whether the frequency is detected. if
% detected, transfer the data to UserData property of the object and stop
% the object

if(ai_continuous_fft(data,plotHandle))
set(obj,'UserData',totalData);
stop(obj);
end
% Updata the title of the graph
set(titleHandle,'String',['Discrete Fourier Transform Plot (fft), Number of callback function calls: ', num2str(count)]);


% The following is for main part
clear all;
close all;

ai = analoginput('winsound');
addchannel(ai,1);
set(ai,'samplesPerTrigger',inf);
daqmem(ai);
set(ai,'timerPeriod',0.5);

% plotting FFT of live input
figure;
P = plot(zeros(1000,1));
T = title(['Discrete Fourier Transform Plot(fft), Number of callback function calls: ', num2str(0)]);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
grid on;
set(ai,'TimerFcn',{@ai_continuous_timer_callback,P,T});

start(ai);

% To keep the code running till the callback issues a stop
while(strcmpi(get(ai,'running'),'On'))
pause(0.5);
end

with this code, I can display the signal in frequency domain live mode. but cannot live displaying in time domain.
I tried wrote another callback function with time domain live plot. however, the code
set(ai,'TimerFcn',{@ai_continuous_timer_callback,P,T}) here, it only allowed to set one callback function at once.

anyone had this experience before? It is very grateful.