Thursday, June 2, 2016

AMPLITUDE MODULATION (AM) AND DEMODULATION

PROGRAM:
clc;
clear all;
close all;

fm = input('Enter the modulating signal frequency(Hz) :');
fc = input('Enter the carrier signal frequency(Hz) :');
fs = input('Enter the sampling frequency(Hz) :');

t = (0:.1:100)'/fs;

sig_mod = sin(2*pi*fm*t);
sig_amod = ammod(sig_mod,fc,fs);
sig_amod_noise = sig_amod + randn(size(t));
sig_admod = amdemod(sig_amod,fc,fs);
sig_admod_noise = amdemod(sig_amod_noise,fc,fs);

subplot(3,2,1);
plot(t,sig_mod);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('MODULATING SIGNAL');

subplot(3,2,3);
plot(t,sig_amod);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('AMPLITUDE MODULATED (AM) SIGNAL');

subplot(3,2,5);
plot(t,sig_admod);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('DEMODULATED AM SIGNAL');

subplot(3,2,2);
plot(t,sig_mod);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('MODULATING SIGNAL');

subplot(3,2,4);
plot(t,sig_amod_noise);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('AMPLITUDE MODULATED SIGNAL WITH ADDED NOISE');

subplot(3,2,6);
plot(t,sig_admod_noise);
xlabel('time (s)->');
ylabel('amplitude (v)->');
title('DEMODULATED NOISE ADDED AM SIGNAL');


SAMPLE OUTPUT:
Enter the modulating signal frequency(Hz) :2
Enter the carrier signal frequency(Hz) :10
Enter the sampling frequency(Hz) :50


PLOT:

AMPLITUDE MODULATION (AM)

PROGRAM:
clc;
clf;
clear;

Fs = 100;
t = (0:2*Fs+1)'/Fs;
Fc = 25;
x = sin(2*pi*t);

ydouble = ammod(x,Fc,Fs);
ysingle = ssbmod(x,Fc,Fs);

subplot(2,2,1);
plot(t,x);
title('Modulating signal');
subplot(2,2,2);
plot(t,ydouble);
title('Double sideband amplitude modulation');
subplot(2,2,3);
plot(t,ysingle);
title('Single sideband amplitude modulation');
subplot(2,2,4);
plot(2*pi*t*Fc,abs(fft(ydouble)),2*pi*t*Fc,abs(fft(ysingle)));
title('Frequency plot');


PLOT:

FREQUENCY MODULATION (FM)

PROGRAM:
clc;
clf;
clear;
close all;

fs = 1000;
t = (0:2*fs+1)'/fs;
fm = 1;
fc = 20;
sm = cos(2*pi*fm*t);
x =  cos(2*pi*fc*t + 10 * sm);


z=demod(x,fc,fs,'fm');
subplot(2,1,1);
plot(t,sm,t,x);

subplot(2,1,2);
plot(t,z);


PLOT:

PULSE AMPLITUDE MODULATION (PAM)

PROGRAM:
clc;
clear all;
close all;
fs = 100;
fm = 1;
%f = input('Enter the signal frequency :');
t=(0:0.05:100)'/fs;
fp = 2;
a = ones(1,length(t));
b = sin(2*pi*t*fm);
y=[];
for i=1:fs/fp:length(t)
    y =  [y,[ones(1,fs/(2*fp)).*b(i),zeros(1,fs/(2*fp))]];   
end
subplot(2,1,1);
plot(t,b);
xlabel('time'),ylabel('amplitude');
title('modulating signal');
subplot(2,1,2)
plot(t,y(1:length(t)));
xlabel('time'),ylabel('amplitude');
title('PAM signal');
axis([0 1 -1 1]);


PLOT:

QUADRATURE PHASE SHIFT KEYING (QPSK)

PROGRAM:
clc;
clf;
clear;
bs = input('Enter the input bit sequence :');
f = input('Enter the modulating signal frequency :');
p = input('Enter the modulating signals phase differences[p2,p3,p4] :');
a = input('Enter the modulating signal amplitude :');
c = 0;
d = 0;
l = length(bs);
if  (mod(l,2) ~= 0)
    bs = [bs,0];
end

% code for generating binary signal
for i = 1:l
    t1 = (c:0.01:c+1);
    x = bs(i) * ones(length(t1),1);
    subplot(2,1,1);
    plot(t1,x);
    axis([0 l -1 2]);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('binary signal');
    hold on
    c=c+1;
end

% code for generating qpsk signal
for i=1:2:l
    t=(d:0.01:d+2);
    if (bs(i)==0 && bs(i+1)==0)
        b = sin(2 * pi * f * t);
    elseif (bs(i)==0 && bs(i+1)==1)
        b = sin(2 * pi * f * t + p(1));
    elseif (bs(i)==1 && bs(i+1)==0)
        b = sin(2 * pi * f * t + p(2));
    elseif (bs(i)==1 && bs(i+1)==1)
        b = sin(2 * pi * f * t + p(3));
    end
    subplot(2,1,2);
    plot(t,b);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('qpsk signal');
    hold on
    d=d+2;
end


SAMPLE OUTPUT:
Enter the input bit sequence :[0 0 0 1 1 0 1 1]
Enter the modulating signal frequency :2
Enter the modulating signals phase differences[p2,p3,p4] :[90 180 270]
Enter the modulating signal amplitude :3


PLOT:

QUADRATURE FREQUENCY SHIFT KEYING (QFSK)

PROGRAM:
clc;
clf;
clear;

bs = input('Enter the input bit sequence :');
f = input('Enter the modulating signal frequencies[f1 f2 f3 f4] :');
a = input('Enter the modulating signal amplitude :');
c = 0;
d = 0;
l = length(bs);
if  (mod(l,2) ~= 0)
    bs = [bs,0];
end

% code for generating binary signal
for i = 1:l
    t1 = (c:0.01:c+1);
    x = bs(i) * ones(length(t1),1);
    subplot(2,1,1);
    plot(t1,x);
    axis([0 l -1 2]);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('binary signal');
    hold on
    c=c+1;
end

% code for generating qfsk signal
for i=1:2:l
    t=(d:0.01:d+2);
    if (bs(i)==0 && bs(i+1)==0)
        b = sin(2 * pi * f(1) * t);
    elseif (bs(i)==0 && bs(i+1)==1)
        b = sin(2 * pi * f(2) * t);
    elseif (bs(i)==1 && bs(i+1)==0)
        b = sin(2 * pi * f(3) * t);
    elseif (bs(i)==1 && bs(i+1)==1)
        b = sin(2 * pi * f(4) * t);
    end
    subplot(2,1,2);
    plot(t,b);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('qfsk signal');
    hold on
    d=d+2;
end


SAMPLE OUTPUT:
Enter the input bit sequence :[0 0 0 1 1 0 1 1]
Enter the modulating signal frequencies[f1 f2 f3 f4] :[1 8 16 32]
Enter the modulating signal amplitude :2


PLOT:

BINARY PHASE SHIFT KEYING (BPSK)

PROGRAM:
clc;
clf;
clear;

bs = input('Enter the input bit sequence :');
f = input('Enter the modulating signal frequency :');
p = input('Enter the modulating signal2 phase difference :');
a = input('Enter the modulating signal amplitude :');

c = 0;
d = 0;
l = length(bs);

% code for generating binary signal
for i = 1:l
    t1 = (c:0.01:c+1);
    x = bs(i) * ones(length(t1),1);
    subplot(2,1,1);
    plot(t1,x);
    axis([0 l -1 2]);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('binary signal');
    hold on
    c=c+1;

end

% code for generating bpsk signal
for i = 1:l
    t=(d:0.01:d+1);
    if (bs(i) == 0)
        b = sin(2 * pi * f * t);
    else
        b = sin(2 * pi * f * t + p);
    end
    subplot(2,1,2);
    plot(t,b);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('bpsk signal');
    hold on
    d=d+1;

end

SAMPLE OUTPUT:
Enter the input bit sequence :[0 1 0 1 1]
Enter the modulating signal frequency :4
Enter the modulating signal2 phase difference :180
Enter the modulating signal amplitude :2


PLOT:

BINARY FREQUENCY SHIFT KEYING (BFSK)

PROGRAM:
clc;
clf;
clear;
bs = input('Enter the input bit sequence :');
f = input('Enter the modulating signal frequencies[f1 f2] :');
a = input('Enter the modulating signal amplitude :');
c = 0;
d = 0;
l = length(bs);

% code for generating binary signal
for i = 1:l
    t1 = (c:0.01:c+1);
    x = bs(i) * ones(length(t1),1);
    subplot(2,1,1);
    plot(t1,x);
    axis([0 l -1 2]);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('binary signal');
    hold on
    c=c+1;
end

% code for generating bfsk signal
for i = 1:l 
    t=(d:0.01:d+1);
    if (bs(i) == 0)
        b = sin(2 * pi * f(1) * t);
    else
        b = sin(2 * pi * f(2) * t);
    end
    subplot(2,1,2);
    plot(t,b);
    xlabel('time ->');
    ylabel('amplitude ->');
    title('bfsk signal');
    hold on
    d=d+1;
end

SAMPLE OUTPUT:
Enter the input bit sequence :[0 1 0 1 1]
Enter the modulating signal frequencies[f1 f2] :[4 15]
Enter the modulating signal amplitude :3

PLOT:

BINARY AMPLITUDE SHIFT KEYING (BASK)

PROGRAM:
clc;
clf;
clear;
bs = input('Enter the input bit sequence :');
f = input('Enter the modulating signal frequency(Hz) :');
a = input('Enter the modulating signal amplitude :');
c = 0;
d = 0;
l = length(bs);

% code for generating binary signal
for i = 1:l   
    t1 = (c:0.01:c+1);
    x = bs(i) * ones(length(t1),1);
    subplot(3,1,1);
    plot(t1,x);
    axis([0 l -1 2]);
    xlabel('time (s) ->');
    ylabel('amplitude (v) ->');
    title('BINARY SIGNAL');
    hold on
    c=c+1;
end

% code for generating modulating signal
mt = (0:0.01:l);
ms = a * sin(2 * pi * f* mt);
subplot(3,1,2);
plot(mt,ms);
xlabel('time (s) ->');
ylabel('amplitude (v) ->');
title('MODULATING SIGNAL');

% code for generating bask signal
for i = 1:l
    t2 = (d:0.01:d+1);
    y = bs(i) * a * sin(2*pi*f*t2);
    subplot(3,1,3);
    plot(t2,y);
    xlabel('time (s) ->');
    ylabel('amplitude (v) ->');
    title('BASK SIGNAL');
    hold on
    d=d+1;
end 

SAMPLE OUTPUT:
Enter the input bit sequence :[0 1 0 1 1]
Enter the modulating signal frequency(Hz) :4
Enter the modulating signal amplitude :2

PLOT:

IIR BUTTERWORTH BANDSTOP FILTER

PROGRAM:
clc;
close all;
clear all;
rp=input('Enter the passband ripple:');
rs=input('Enter the stopband ripple:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'stop');
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(211);
plot(om/pi,m);
ylabel('Gain in dB--->');
xlabel('Normalised frequency--->');
title('A. Magnitude Plot');
subplot(212);
plot(om/pi,an);
ylabel('Phase in radians--->');
xlabel('Normalised frequency--->');
title('B. Phase Plot');

 
SAMPLE OUTPUT:
Enter the passband ripple: 0.2
Enter the stopband ripple:75
Enter the passband frequency:1000
Enter the stopband frequency: 2500
Enter the sampling frequency: 7000

PLOT:



IIR BUTTERWORTH BANDPASS FILTER

PROGRAM: clc;
close all;
clear all;
rp=input('Enter the passband ripple:');
rs=input('Enter the stopband ripple:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'bandpass');
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(211);
plot(om/pi,m);
ylabel('Gain in dB--->');
xlabel('Normalised frequency--->');
title('A. Magnitude Plot');
subplot(212);
plot(om/pi,an);
ylabel('Phase in radians--->');
xlabel('Normalised frequency--->');
title('B. Phase Plot');

 
SAMPLE OUTPUT:
Enter the passband ripple: 0.3
Enter the stopband ripple: 40
Enter the passband frequency:1500
Enter the stopband frequency: 2000
Enter the sampling frequency: 9000

PLOT:



IIR BUTTERWORTH HIGHPASS FILTER

PROGRAM: clc;
close all;
clear all;
rp=input('Enter the passband ripple:');
rs=input('Enter the stopband ripple:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn,'high');
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(211);
plot(om/pi,m);
ylabel('Gain in dB--->');
xlabel('Normalised frequency--->');
title('A. Magnitude Plot');
subplot(212);
plot(om/pi,an);
ylabel('Phase in radians--->');
xlabel('Normalised frequency--->');
title('B. Phase Plot');

 
SAMPLE OUTPUT:
Enter the passband ripple: 0.5
Enter the stopband ripple: 50
Enter the passband frequency:2400
Enter the stopband frequency: 1200
Enter the sampling frequency: 10000

PLOT:



IIR BUTTERWORTH LOWPASS FILTER

PROGRAM: clc;
clear all;
close all;
rp=input('Enter the passband ripple:');
rs=input('Enter the stopband ripple:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wn);
w=0:.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(211);
plot(om/pi,m);
ylabel('Gain in dB--->');
xlabel('Normalised frequency--->');
title('A. Magnitude Plot');
subplot(212);
plot(om/pi,an);
ylabel('Phase in radians--->');
xlabel('Normalised frequency--->');
title('B. Phase Plot');

SAMPLE OUTPUT:
Enter the passband ripple: 0.5
Enter the stopband ripple: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000

PLOT: