I want to generate a continous pulse in Matlab. I would like to generate the signal at 5khz with a duration of 0.01s, then nothing for 0.09s and then starting again. It's kind of a rectangular pulse, except it's in 5khz.
I have the following code to output a waveform for 0.01s at 5khz,
function [ output ] = FreqGen( )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
fs = 44100;
T = 0.01;
t = 0:(1/fs):T;
f = 5000;
a = 0.5;
output = a*sin(2*pi*f*t);
end
but I failed to figure out how to use Matlab function pulsetran to generate 0.09s pulses.
Just like the plot below:
The documentation of pulstran is not really helpful. While one could look into the function directly it is actually the easiest way to implement what you want yourself (and you circumvent the Signal Processing Toolbox). Here I did it:
The rectangular envelope is calculated with the help of modulo and a comparison.
And it looks like:
Now with
pulstran
.A short documentation on
pulstran
. The syntax is:and
t
are time steps for calculating the pulses (also the total time and dimensions of output),d
are the pulse centers (shifting deltas) andp1, p2, ..
are additional parameters to the function.So the output is similar to the sum of
function(t+d(i), p1, p2, ..)
over all elements ofd
.Here is the code:
It's a bit slower though than the previous answer.