www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/FilteringFramesOfANoisySineWaveSignalExample.m

    %% Filter Frames of a Noisy Sine Wave Signal in MATLAB
% This example shows how to lowpass filter a noisy signal in MATLAB and 
% visualize the original and filtered signals using a spectrum analyzer. 
% For a Simulink version of this example, see
% <matlab:web(fullfile(docroot,'dsp/ug/filter-frames-of-a-noisy-sine-wave-signal-in-simulink.html')) Filter Frames of a Noisy Sine Wave Signal in Simulink>

%% Specify Signal Source 
% The input signal is the sum of two sine waves with frequencies of 1 kHz 
% and 10 kHz. The sampling frequency is 44.1 kHz. 
Sine1 = dsp.SineWave('Frequency',1e3,'SampleRate',44.1e3);
Sine2 = dsp.SineWave('Frequency',10e3,'SampleRate',44.1e3);

%% Create Lowpass Filter
% The lowpass FIR filter, |dsp.LowpassFilter|, designs a minimum-order 
% FIR lowpass filter using the generalized Remez FIR filter design algorithm.
% Set the passband frequency to 5000 Hz and the stopband frequency to 8000 Hz.
% The passband ripple is 0.1 dB and the stopband attenuation is 80 dB.
FIRLowPass = dsp.LowpassFilter('PassbandFrequency',5000,...
    'StopbandFrequency',8000);

%% Create Spectrum Analyzer
% Set up the spectrum analyzer to compare the power spectra of the original
% and filtered signals. The spectrum units are dBm.
SpecAna = dsp.SpectrumAnalyzer('PlotAsTwoSidedSpectrum',false, ...
    'SampleRate',Sine1.SampleRate, ...
    'NumInputPorts',2,...
    'ShowLegend',true, ...
    'YLimits',[-145,45]);

SpecAna.ChannelNames = {'Original noisy signal','Low pass filtered signal'};

%% Specify Samples per Frame
% This example uses frame-based processing, where data is processed one 
% frame at a time. Each frame of data contains sequential samples from an 
% independent channel. Frame-based processing is advantageous for many 
% signal processing applications because you can process multiple samples 
% at once. By buffering your data into frames and processing multisample 
% frames of data, you can improve the computational time of your signal 
% processing algorithms. Set the number of samples per frame to 4000. 
Sine1.SamplesPerFrame = 4000;
Sine2.SamplesPerFrame = 4000;

%% Filter the Noisy Sine Wave Signal
% Add zero-mean white Gaussian noise with a standard deviation of 0.1 to
% the sum of sine waves. Filter the result using the FIR filter. While
% running the simulation, the spectrum analyzer shows that frequencies
% above 8000 Hz in the source signal are attenuated. The resulting signal
% maintains the peak at 1 kHz because it falls in the passband of the
% lowpass filter.
for i = 1 : 1000
    x = Sine1()+Sine2()+0.1.*randn(Sine1.SamplesPerFrame,1);
    y = FIRLowPass(x);    
    SpecAna(x,y);                              
end
release(SpecAna)