%% Upsample Signal
% Convert a signal from 40 MHz to 100 MHz by using the dsp.HDLFIRRateConverter System object™. To avoid overrunning the object as the signal is upsampled, control the input rate manually.
% Define the sample rate and length of the input signal, and a fixed-point cosine waveform.

Fs = 40e6;                  
Ns = 50;                  
t = (0:Ns-1).'/Fs;         
x = fi(cos(2*pi*1.2e6*t),1,16,14);

%
% Define the rate conversion parameters. Use an interpolation factor of 5 and a decimation factor of 2. Calculate how often the object can accept a new input sample.

L = 5;   
M = 2;   
stepsPerInput = ceil(L/M);
numSteps = stepsPerInput*Ns;

%
% Generate dataIn and validIn based on how often the object can accept a new sample.

dataIn = zeros(numSteps,1,'like',x);
dataIn(1:stepsPerInput:end) = x;
validIn = false(numSteps,1);
validIn(1:stepsPerInput:end) = true;

%
% Create the System object. Configure it to perform rate conversion using the specified factors and an equiripple FIR filter.

Numerator = firpm(70,[0 0.15 0.25 1],[1 1 0 0]);         
rateConverter = dsp.HDLFIRRateConverter(L,M,Numerator);

%
% Create a Logic Analyzer to capture and view the input and output signals.

la = dsp.LogicAnalyzer('NumInputPorts',4,'SampleTime',1/Fs,'TimeSpan',numSteps/Fs);
modifyDisplayChannel(la,1,'Name','dataIn','Format','Analog','Height',8)
modifyDisplayChannel(la,2,'Name','validIn')
modifyDisplayChannel(la,3,'Name','dataOut','Format','Analog','Height',8)
modifyDisplayChannel(la,4,'Name','validOut')

%
% Call the System object to perform the rate conversion and obtain each output sample. Call the Logic Analyzer to add each sample to the waveform display.
% Note: This object syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent step syntax. For example, replace myObject(x) with step(myObject,x).

for k = 1:numSteps
    [dataOut,validOut] = rateConverter(dataIn(k),validIn(k));    
    la(dataIn(k),validIn(k),dataOut,validOut)    
end

%
% Copyright 2012 The MathWorks, Inc.