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

    %% Downsample Signal 
% Convert a  signal from 48 kHz to 32 kHz using the HDL FIR Rate Converter
% System object(TM).
%%
% Define the sample rate and length of the input signal, and a 2 kHz cosine
% waveform. Set |validIn| = |true| for every sample. 
Fs = 48e3;                  
Ns = 100;                  
t = (0:Ns-1).'/Fs;         
dataIn = cos(2*pi*2e3*t);  
validIn = true(Ns,1);       
%%
% Preallocate |dataOut| and |validOut| signals for faster simulation.
dataOut = zeros(Ns,1);
validOut = false(Ns,1);
%%
% Create the System object. Configure it to perform rate conversion 
% by a factor of 2/3, using an equiripple filter.
Numerator = firpm(70, [0,.25,.32,1], [1,1,0,0]);
firrc = dsp.HDLFIRRateConverter(2,3,Numerator);
%%
% Execute the |step| method of the System object to perform the rate 
% conversion and obtain each output sample.
for k = 1:Ns
    [dataOut(k),validOut(k)] = step(firrc,dataIn(k),validIn(k));
end
%%
% Because the input sample rate is higher than the output sample rate, not 
% every member of |dataOut| is valid. Use |validOut| to extract the valid 
% samples from |dataOut|.
y = dataOut(validOut);
%%
% View the input and output signals with the *Logic Analyzer*.
la = dsp.LogicAnalyzer('NumInputPorts',4,'SampleTime',1/Fs,'TimeSpan',Ns/Fs);
addWave(la,'InputChannel',1,'Name','dataIn','Format','Analog','Height',5)
addWave(la,'InputChannel',2,'Name','validIn')
addWave(la,'InputChannel',3,'Name','dataOut','Format','Analog','Height',5)
addWave(la,'InputChannel',4,'Name','validOut')
step(la,dataIn,validIn,dataOut,validOut)