introduction to streaming signal processing in matlab -凯发k8网页登录
use system objects to process streaming signals in matlab®. the signals are read in and processed frame by frame (or block by block) in each processing loop. you can control the size of each frame.
in this example, frames of 1024 samples are filtered using a notch-peak filter in each processing loop. the input is a sine wave signal that is streamed frame by frame from a dsp.sinewave
object. the filter is a notch-peak filter created using a dsp.notchpeakfilter
object. to ensure smooth processing as each frame is filtered, the system objects maintain the state of the filter from one frame to the next automatically.
initialize streaming components
initialize the sine wave source to generate the sine wave, the notch-peak filter to filter the sine wave, and the spectrum analyzer to show the filtered signal. the input sine wave has two frequencies: one at 100 hz, and the other at 1000 hz. create two dsp.sinewave
objects, one to generate the 100 hz sine wave, and the other to generate the 1000 hz sine wave.
fs = 2500; sineobject1 = dsp.sinewave('samplesperframe',1024,... 'samplerate',fs,'frequency',100); sineobject2 = dsp.sinewave('samplesperframe',1024,... 'samplerate',fs,'frequency',1000); sa = spectrumanalyzer('samplerate',fs,... 'method','welch',... 'averagingmethod','exponential',... 'forgettingfactor',0.1,... 'plotastwosidedspectrum',false,... 'channelnames',{'sinewaveinput','notchoutput'},'showlegend',true);
create notch-peak filter
create a second-order iir notch-peak filter to filter the sine wave signal. the filter has a notch at 750 hz and a q-factor of 35. a higher q-factor results in a narrower 3-db bandwidth of the notch. if you tune the filter parameters during streaming, you can see the effect immediately in the spectrum analyzer output.
wo = 750; q = 35; bw = wo/q; notchfilter = dsp.notchpeakfilter('bandwidth',bw,... 'centerfrequency',wo, 'samplerate',fs); fvtool(notchfilter);
stream in and process signal
construct a for-loop to run for 3000 iterations. in each iteration, stream in 1024 samples (one frame) of the sinewave and apply a notch filter on each frame of the input signal. to generate the input signal, add the two sine waves. the resultant signal is a sine wave with two frequencies: one at 100 hz and the other at 1000 hz. the notch of the filter is tuned to a frequency of 100, 500, 750, or 1000 hz, based on the value of vecindex
. the filter bandwidth changes accordingly. when the filter parameters change during streaming, the output in the spectrum analyzer gets updated accordingly.
freqvec = [100 500 750 1000]; vecindex = 1; vecelem = freqvec(vecindex); for iter = 1:3000 sinewave1 = sineobject1(); sinewave2 = sineobject2(); input = sinewave1 sinewave2; if (mod(iter,350)==0) if vecindex < 4 vecindex = vecindex 1; else vecindex = 1; end vecelem = freqvec(vecindex); end notchfilter.centerfrequency = vecelem; notchfilter.bandwidth = notchfilter.centerfrequency/q; output = notchfilter(input); sa(input,output); end
fvtool(notchfilter)
at the end of the processing loop, the centerfrequency
is at 100 hz. in the filter output, the 100 hz frequency is completely nulled out by the notch filter, while the frequency at 1000 hz is unaffected.