%% Envelope Spectrum of Modulated Pulses
% Generate a signal sampled at 1 kHz for 5 seconds. The signal consists of 0.01-second rectangular pulses that repeat every T = 0.25 second. Amplitude modulate the signal onto a sinusoid of carrier frequency 150 Hz.

fs = 1e3;
tmax = 5;

t = 0:1/fs:tmax;
y = pulstran(t,0:0.25:tmax,'rectpuls',0.01);

fc = 150;
z = modulate(y,fc,fs);

%
% Plot the original and modulated signals. Show only the first few cycles.

plot(t,y,t,z,'-')
grid on
axis([0 1 -1.1 1.1])

%
% Compute the envelope and envelope spectrum of the signal. Determine the signal envelope using complex demodulation. Compute the envelope spectrum on a 20 Hz interval centered at the carrier frequency.

[q,f,e,te] = envspectrum(z,fs,'Method','demod','Band',[fc-10 fc+10]);

%
% Plot the envelope signal and the envelope spectrum. Zoom in on the interval from 0 to 50 Hz.

subplot(2,1,1)
plot(te,e)
xlabel('Time')
title('Envelope')

subplot(2,1,2)
plot(f,q)
xlim([0 50])
xlabel('Frequency')
title('Envelope Spectrum')

%
% The envelope signal has the same period in time, T = 0.25 second, as the original signal. The envelope spectrum has pulses at 1 / T = 4 Hz.
% Repeat the computation, but now use the hilbert function to compute the envelope. Bandpass-filter the signal using a 10th-order finite impulse response (FIR) filter. Plot the envelope signal and envelope spectrum using the built-in functionality of envspectrum.

envspectrum(z,fs,'Method','hilbert','FilterOrder',10)

%
% Embed the signal in white Gaussian noise of variance 1/3. Plot the result.

zn = z + randn(size(z))/3;

plot(t,zn,'-')
grid on
axis([0 1 -1.1 1.1])

%
% Compute and display the envelope signal and envelope spectrum. Compute the envelope spectrum using complex demodulation on a 10 Hz interval centered at the carrier frequency. Zoom in on the interval from 0 to 50 Hz.

envspectrum(zn,fs,'Band',[fc-5 fc+5])
xlim([0 50])

% Copyright 2012 The MathWorks, Inc.