0% found this document useful (0 votes)
3 views9 pages

Softwareanalogcode

The document contains MATLAB code for generating and analyzing various modulation techniques including PWM, AM, FM, and PM signals. It demonstrates the creation of these signals, their modulation, and subsequent demodulation processes, along with filtering and noise addition. Several plots are generated to visualize the original and demodulated signals for comparison.

Uploaded by

sharonanto06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Softwareanalogcode

The document contains MATLAB code for generating and analyzing various modulation techniques including PWM, AM, FM, and PM signals. It demonstrates the creation of these signals, their modulation, and subsequent demodulation processes, along with filtering and noise addition. Several plots are generated to visualize the original and demodulated signals for comparison.

Uploaded by

sharonanto06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

clc

clear all

close all

fs=1000;

t=0:1/fs:1;

fm=5;

fc=50;

m=cos(2*pi*fm*t);

c=sawtooth(2*pi*fc*t);

pwm=m>c;

[b,a]=butter(5,(2*fm)/(fs/2));

dpwm=filter(b,a,pwm);

ppm=[0 diff(pwm)];

% align ppm length to t (ppm is one element shorter due to diff)

ppm_plot = [ppm, 0]; % append zero to match length of t

% create a new figure for ppm or overlay on existing plots; here overlay on second subplot

subplot(2,1,2);

hold on;

stairs(t,ppm_plot,'m','LineWidth',1);

legend('Filtered PWM (dpwm)','PPM (ppm)');

hold off;

figure;

subplot(2,1,1);

plot(t,pwm,'k');

ylim([-0.2 1.2]);

xlabel('Time (s)');

ylabel('PWM');

title('PWM Signal');

grid on;

subplot(2,1,2);
plot(t,dpwm,'b');

xlabel('Time (s)');

ylabel('Filtered PWM');

title('Filtered PWM (dpwm)');

grid on;

% also plot the carrier 'c' on the first subplot for comparison

subplot(2,1,1);

hold on;

plot(t,c,'r--','LineWidth',1);

legend('PWM','Carrier (c)');

hold off;

clc

close all

clear all

fs=1000;

t=0:1/fs:1;

fc=100;

fm=5;

kp=1;

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

s_t=cos(2*pi*fc*t+kp*m);

theta=0;

vco=zeros(size(t));

phase_error=zeros(size(t));

for i=2:length(t)

vco(i)=cos(theta);

phase_error(i)=s_t(i)*vco(i);

theta=theta+2*pi*fc/fs+0.01*phase_error(i);

end

figure
subplot(4,1,1)

plot(t,s_t)

subplot(4,1,2)

plot(t,vco)

subplot(4,1,3)

plot(t,phase_error)

subplot(4,1,4)

plot(t,s_t,'b')

hold on

plot(t,vco,'r--')

clc

clear all

close all

fs=200;

t=0:1/fs:1;

fm_freq=20;

fc=80;

x=cos(2*pi*fm_freq*t);

c=cos(2*pi*fc*t)

Ac=1;

am=(1+0.8*x).*c;

fm_sig=cos(2*pi*fc*t+10*sin(2*pi*fm_freq*t));

pm=cos(2*pi*fc*t+5*x);
noise_amp=0.2;

n=noise_amp*randn(size(t));

am_n=am+n;

fm_n=fm_sig+n;

pm_n=pm+n;

%% AM demodulation

z_am=abs(hilbert(am))-1;

z_am_n=abs(hilbert(am_n))-1;

%% FM demodulation

z_fm=[0 diff(unwrap(angle(hilbert(fm_sig))))];

z_fm_n=[0 diff(unwrap(angle(hilbert(fm_n))))];

%% PM demodulation

z_pm=unwrap(angle(hilbert(pm))) - 2*pi*fc*t;

z_pm_n=unwrap(angle(hilbert(pm_n))) - 2*pi*fc*t;

%% Plotting

figure

subplot(4,2,1)

plot(t,am)

title('AM Signal')

subplot(4,2,2)
plot(t,am_n)

title('Noisy AM')

subplot(4,2,3)

plot(t,fm_sig)

title('FM Signal')

subplot(4,2,4)

plot(t,fm_n)

title('Noisy FM')

subplot(4,2,5)

plot(t,z_am)

title('Recovered AM')

subplot(4,2,6)

plot(t,z_am_n)

title('Recovered Noisy AM')

subplot(4,2,7)

plot(t,z_fm)

title('Recovered FM')

subplot(4,2,8)

plot(t,z_fm_n)

title('Recovered Noisy FM')

clc

clear all

close all
fs=1000;

t=0:1/fs:1;

fm=5;

fc=50;

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

c=0.5*square(2*pi*fc*t)+0.5;

spam=m.*c;

[b,a]=butter(5,(2*fm)/(fs/2));

demod_pam=filter(b,a,spam);

dmod_pam=2*demod_pam;

figure

subplot(4,1,1)

plot(t,spam)

subplot(4,1,2)

plot(t,demod_pam)

clc

clear all

close all

fs=1000;

t=0:1/fs:1;

m=sin(2*pi*5*t);

c=sawtooth(2*pi*10*t);

pwm=m>c;

figure

subplot(2,1,1)

plot(t,pwm)

subplot(2,1,2)

plot(t,c)
clc

clear all

close all

fs=10000;

t=0:1/fs:0.1;

m=cos(2*pi*35*t);

c=cos(2*pi*500*t);

mf=10;

mp=2;

snr=10;

fc=500;

fm_m=cos(2*pi*500*t+10*sin(2*pi*35*t));

dem_fm=demod(fm_m,fc,fs,'fm');

pm_m=cos(2*pi*500*t+2*m);

dem_pm_m=demod(pm_m,fc,fs,'pm');

y_fm=awgn(fm_m,snr);

y_dem_fm=awgn(dem_fm,snr);

y_pm_m=awgn(pm_m,snr);

y_dem_pm=awgn(dem_pm_m,snr);

figure

subplot(8,2,1)

plot(t,fm_m)

subplot(8,2,2)

plot(t,dem_fm)

subplot(8,2,3)

plot(t,pm_m)

subplot(8,2,4)

plot(t,dem_pm_m)

subplot(8,2,5)

plot(t,y_fm)

subplot(8,2,6)
plot(t,y_dem_fm)

subplot(8,2,7)

plot(t,y_pm_m)

subplot(8,2,8)

plot(t,y_dem_pm)

clc

clear all

close all

fs=1000;

t=0:1/fs:1;

Ac=5;

Am=2;

fm=10;

fc=100;

m=Am*cos(2*pi*fm*t);

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

am=m.*c;

dm=am.*c;

am_m=ammod(m,fc,fs,0,Ac);

dm_m=amdemod(m,fc,fs,0,Ac);

[b,a]=butter(1,0.01);

demod=filter(b,a,dm);

figure

subplot(6,1,1)

plot(t,am)

subplot(6,1,2)

plot(t,dm)

subplot(6,1,3)

plot(t,am_m)

subplot(6,1,4)
plot(t,dm_m)

subplot(6,1,5)

plot(t,demod)

You might also like