Ac Lab Matlab Codes
Ac Lab Matlab Codes
fm=20;
fc=500;
vm=1;
vc=1;
mu=0.1;
t=0:0.00001:0.0999;
f=0:1:9999;
m=vm*cos(2*pi*fm*t); %% message
mp=vm*sin(2*pi*fm*t);
c=vc*cos(2*pi*fc*t); %% carrier
cp=vc*sin(2*pi*fc*t);
ss1=m.*c;
ss2=mp.*cp;
upper=ss1-ss2; %% upper sideband signal
lower=ss1+ss2; %% lower sideband signal
Vfupper=abs(fft(upper,10000))/10000; %% upper frequency spectrum
Vflower=abs(fft(lower,10000))/10000; %% lower frequency spectrum
%% lower demodulated
vldemod=c.*lower;
[b a]=butter(2,0.002);
lowerdemod=filter(b,a,vldemod);
figure(1)
subplot(211);plot(t,m)
xlabel('Time') ;ylabel('Amplitude');
title('Message signal');grid;
subplot(212);plot(t,c)
xlabel('Time');ylabel('Amplitude');
title('Carrier signal');grid;
figure(2)
subplot(211);plot(t,upper)
xlabel('Time');ylabel('Amplitude');
title('SSB Upper Sideband signal');grid
subplot(212);plot(t,lower)
xlabel('Time');ylabel('Amplitude');
title('SSB Lower Sideband signal');grid
figure(3)
subplot(211);plot(f*10,Vfupper)
axis([(fc-20*fm) (fc+20*fm) 0 0.6]);
xlabel('Frequency');ylabel('Power');
title('SSB Upper Sidebsnd signal spectrum');grid
subplot(212);plot(f*10,Vflower)
axis([(fc-20*fm) (fc+20*fm) 0 0.6]);
xlabel('Frequency');ylabel('Power');
title('SSB Lower Sidebsnd signal spectrum');grid
figure(4)
subplot(211);plot(t,upperdemod)
xlabel('Time') ;ylabel('Amplitude');
title('Upper sideband Demodulated signal');grid;
subplot(212);plot(t,lowerdemod)
xlabel('Time') ;ylabel('Amplitude');
title('Lower sideband Demodulated signal');grid;
7. Time Division multiplexing and de-multiplexing using MATLAB
MATLAB CODE:
clc;
close all;
clear all;
% Signal generation
x=0:.5:4*pi; % siganal taken upto 4pi
l1=length(sig1);
l2=length(sig2);
for i=1:l1
sig(1,i)=sig1(i); % Making Both row vector to a matrix
sig(2,i)=sig2(i);
end
MATLAB CODE:
clc;
clear all;
close all;
fc=100;
fm=fc/10;
fs=100*fc;
t=0:1/fs:4/fm;
mt=cos(2*pi*fm*t);
ct=0.5*square(2*pi*fc*t)+0.5;
st=mt.*ct;
tt=[ ];
figure(1)
subplot(4,1,1);
plot(t,mt); title('message signal'); xlabel('timeperiod'); ylabel('amplitude');
subplot(4,1,2);
plot(t,ct); title('carrier signal'); xlabel('timeperiod');ylabel('amplitude');
subplot(4,1,3); plot(t,st); title('BIPOLAR PAM');xlabel('timeperiod');
ylabel('amplitude');
subplot(4,1,4); plot(t,tt); title('PAM'); xlabel('timeperiod'); ylabel('amplitude');
%demodulation
dt=st.*ct;
dt_frequency=fftshift(abs(fft(dt)));
filter=fir1(200,fm/fs,'low');
original_t_signal=conv(filter,dt);
original_f_signal=fftshift(abs(fft(original_t_signal)));
t1=0:1/(length(original_t_signal)-1):1;
f=-fs/2:fs/(length(original_f_signal)-1):fs/2;
figure(2)
subplot(2,1,1); plot(t1,original_t_signal); title('time domain signal');
xlabel('timeperiod'); ylabel('amplitude');
subplot(2,1,2); plot(f,original_f_signal); title('frequency domain signal');
xlabel('frequency'); ylabel('amplitude');
axis([-50 50 0 2000]);
9 PPM Signal Generation and Demodulation using MATLAB
MATLAB CODE:
clc;
clear all;
close all;
fc=4000;
fs=40000;
fm=1000;
t=0:1/fs:(2/fm-1/fs);
mt=0.4*sin(2*pi*fm*t)+0.5;
st=modulate(mt,fc,fs,'PPM');
dt=demod(st,fc,fs,'PPM');
figure
subplot(3,1,1); plot(mt); title('message signal');
xlabel('timeperiod'); ylabel('amplitude'); axis([0 50 0 1])
subplot(3,1,2); plot(st); title(' PPM signal');
xlabel('timeperiod'); ylabel('amplitude'); axis([0 500 -0.2 1.2])
subplot(3,1,3); plot(dt); title(' PPM demodulated signal');
xlabel('timeperiod'); ylabel('amplitude');axis([0 50 0 1])
10 AGC Characteristics of Radio Receiver using MATLAB
MATLAB CODE:
clc
Fs = 100e3; %sampling freq
t = 0:1/Fs:.1-1/Fs; % time variable
Am=2;
fm = 200; %fm 200 Hz
% am modulation
Ac = 8;
c=Ac.*cos(2*pi*Fc*t); %carrier signal
figure;
% ploting message and carrier signals
subplot(2,1,1);
plot(c);
title('carrier'); xlabel('time'); ylabel('amplitude');
subplot(2,1,2); plot(m); title('message'); xlabel('time'); ylabel('amplitude');
figure;
f=1000;%Carrier frequency
fs=100000;%Sample frequency
N=5000;%Number of samples
Ts=1/fs;
t=(0:Ts:(N*Ts)- Ts);
%Create the message signal
f1=100;%Modulating frequency
msg=sin(2*pi*f1*t);
kf=.0628;%Modulation index
%PLL implementation
for n=2:length(Signal)
vco(n)=conj(exp(j*(2*pi*n*f/fs+phi_hat(n-1))));%Compute VCO
phd_output(n)=imag(Signal(n)*vco(n));%Complex multiply VCO x Signal input
e(n)=e(n-1)+(kp+ki)*phd_output(n)-ki*phd_output(n-1);%Filter integrator
phi_hat(n)=phi_hat(n-1)+e(n);%Update VCO
end;
%Plot waveforms
startplot = 1;
endplot = 1000;
figure(1); subplot(3,2,1);
plot(t(startplot:endplot), msg(startplot:endplot)); title('100 Hz message signal');
xlabel('Time (seconds)'); ylabel('Amplitude'); grid;
figure(1); subplot(3,2,2);
plot(t(startplot:endplot), real(Signal(startplot:endplot))); title('FM (1KHz carrier
modulated with a 100 Hz message signal)');
xlabel('Time (seconds)'); ylabel('Amplitude'); grid;
figure(1);
subplot(3,2,3);
plot(t(startplot:endplot), e(startplot:endplot)); title('PLL Loop Filter/Integrator
Output');
xlabel('Time (seconds)'); ylabel('Amplitude'); grid;
subplot(3,2,4);
plot(t(startplot:endplot), real(vco(startplot:endplot))); title('VCO Output (PLL
tracking the input signal)');
%xlabel('Time (seconds)'); ylabel('Amplitude'); grid;
subplot(3,2,5);
plot(t(startplot:endplot), phd_output(startplot:endplot)); title('Phase Detecter
Output');
xlabel('Time (seconds)'); ylabel('Amplitude'); grid;
subplot(3,2,6);
plot(t(startplot:endplot), real(Signal1(startplot:endplot)));
title('Unmodulated Carrier');
xlabel('Time (seconds)'); ylabel('Amplitude'); grid;