%% Custom Pulse Trains
% Write a function that generates custom pulses consisting of a sinusoid damped by an exponential. The pulse is an odd function of time. The generating function has a second input argument that specifies a single value for the sinusoid frequency and the damping factor. Display a generated pulse, sampled at 1 kHz for 1 second, with a frequency and damping value, both equal to 30.

fnx = @(x,fn) sin(2*pi*fn*x).*exp(-fn*abs(x));

ffs = 1000;
tp = 0:1/ffs:1;

pp = fnx(tp,30);

plot(tp,pp)
xlabel('Time (s)')
ylabel('Waveform')

% Use the pulstran function to generate a train of custom pulses. The train is sampled at 2 kHz for 1.2 seconds. The pulses occur every third of a second and have exponentially decreasing amplitudes.
% Initially specify the generated pulse as a prototype. Include the prototype sample rate in the function call. In this case, pulstran replicates the pulses at the specified locations.

fs = 2e3;
t = 0:1/fs:1.2;

d = 0:1/3:1;
dd = [d;4.^-d]';

z = pulstran(t,dd,pp,ffs);

plot(t,z)
xlabel('Time (s)')
ylabel('Waveform')

%
% Generate the pulse train again, but now use the generating function as an input argument. Include the frequency and damping parameter in the function call. In this case, pulstran generates the pulse so that it is centered about zero.

y = pulstran(t,dd,fnx,30);

plot(t,y)
xlabel('Time (s)')
ylabel('Waveform')

% Copyright 2018 The MathWorks, Inc.