MATLAB Code for STD AM Envelope Detection

Shubham Gupta
1 min readJun 26, 2021

clc;

clear all;

% STD AM envelope detection. The message signal

% is +1 for 0 < t < t0/3

%-2 for t0/3 < t < 2t0/3 and zero otherwise.

fm=10; %Message signal Frequency

Am=1.5; % Amplitude of Message Signal

fc=250; % Carrier Signal Frequency

Ac=10; % Amplitude of Carrier Signal

fs=1000; % Sampling Frequency

Ts=1/fs; % Sampling Period

N=10000; % Length of Signal

t=0:1/(fs):(N-1)*Ts; % Time Vector

index=0.85;

y1=sin(2*pi*fm*t);

msg=Am*[(y1>0)-(y1<0)]-0.5;

for i=0:(N-1)*Ts*fs

if(i>fs/fm)

msg(i+1)=0;

end

end

carrier=Ac*cos(2*pi*fc*t);

msg_nor=msg/max(abs(msg)); %Normalized Message

mod=(1+index*msg_nor).*carrier; %AM Modulation

env=abs(hilbert(mod))/Ac;

demod=2*(env-1)/index;

signal_power=(norm(mod)²)/length(mod); % power in modulated signal

noise_power=0.01*signal_power; % noise power

noise_std=sqrt(noise_power); % noise standard deviation

noise=noise_std*randn(1,length(mod)); % generate noise

mod_noise=mod+noise;

env2=abs(hilbert(mod_noise))/Ac;

demod2=2*(env2–1)/index;

subplot(4,2,1);

plot(t,msg);

ylabel(‘Amplitude’);

title(‘Message ‘);

axis([0,0.5,-3,3]);

subplot(4,2,2);

plot(t,carrier);

xlabel(‘Time’);

ylabel(‘Amplitude’);

title(‘Carrier ‘);

axis([0,0.1,-12,12]);

subplot(4,2,3);

plot(t,mod);

xlabel(‘Time’);

title(‘AM Modulated Signal ‘);

axis([0,0.2,-22,22]);

Y=fft(mod);

P2 = abs(Y/N);%Two Sided Spectrum

P1 = P2(1:N/2+1);%One Sided Spectrum

P1(2:end-1) = 2*P1(2:end-1);

df=fs/N;

f = 0:df:N*df/2;

subplot(4,2,4);

plot(f,P1);

ylabel(‘Amplitude ‘);

title(‘Fourier of AM Modulated’);

axis([100,450,0,0.1]);

subplot(4,2,6);

plot(t,demod);

xlabel(‘Time’);

title(‘AM Demodulated Signal ‘);

axis([0,0.2,-3,2]);

subplot(4,2,5);

plot(t,env);

xlabel(‘Time’);

title(‘Envelope of Signal ‘);

subplot(4,2,7);

plot(t,noise);

xlabel(‘Time’);

title(‘AGWN Noise ‘);

axis([0,2,-2,3]);

subplot(4,2,8);

plot(t,demod2);

xlabel(‘Time’);

title(‘Demodulated Signal in presence of noise ‘);

axis([0,0.2,-3,2]);

--

--

Shubham Gupta

Software Engineer and Content Writer at Lovebird Lingerie