Untitled

 avatar
unknown
plain_text
2 years ago
9.6 kB
4
Indexable
%pcm------------------

clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system :  ');
n1=input('Enter number of samples in a period : ');
L=2^n;
% Sampling Operation
x=0:2*pi/n1:4*pi;               
s=10*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(s);
grid on;
title('Sampled Signal 21BEC1308');
ylabel('Amplitude');
xlabel('Time');

 %  Quantization Process
 vmax=8;
 vmin=-vmax;
 del=(vmax-vmin)/L;
 part=vmin:del:vmax;                              
 code=vmin-(del/2):del:vmax+(del/2);        
 [ind,q]=quantiz(s,part,code);                  
                                                                     
 l1=length(ind);
 l2=length(q);
  
 for i=1:l1
    if(ind(i)~=0)                                            % To make index as binary decimal so started from 0 to N
       ind(i)=ind(i)-1;
    end 
    i=i+1;
 end   
  for i=1:l2
     if(q(i)==vmin-(del/2))                          % To make quantize value inbetween the levels
         q(i)=vmin+(del/2);
     end
 end    
 subplot(3,1,3);
 stem(q);grid on;                                       % Display the Quantize values
 title('Quantized Signal');
 ylabel('Amplitude--->');
 xlabel('Time--->');
  
 %  Encoding Process
 figure
 code=de2bi(ind,'left-msb');             % Cnvert the decimal to binary
 k=1;
for i=1:l1
    for j=1:n
        coded(k)=code(i,j);                  % convert code matrix to a coded row vector
        j=j+1;
        k=k+1;
    end
    i=i+1;
end
 subplot(2,1,1); grid on;
 stairs(coded);                                 % Display the encoded signal
axis([0 100 -2 3]);  title('Encoded Signal');
 ylabel('Amplitude--->');
 xlabel('Time--->');
 
 %   Demodulation Of PCM signal
 
 qunt=reshape(coded,n,length(coded)/n);
 index=bi2de(qunt','left-msb');                    % Getback the index in decimal form
 q=del*index+vmin+(del/2);                       % getback Quantized values
 subplot(2,1,2); grid on;
 plot(q);                                                        % Plot Demodulated signal
 title('Demodulated Signal');
 ylabel('Amplitude--->');
 xlabel('Time--->');

 % DPCM Transmitter and Receiver
clc;
clear all;
close all;
fm=4; % Input signal frequncy
fs=20*fm % Sampling Frequency
am=2; % Input signal Amplitude
t= 0:1/fs:1; % Time
x =am*cos(2*pi*fm*t); % Input Sinusoidal Signal
figure;
subplot(2,1,1);
plot(t,x,'r -');
xlabel('Time');
ylabel('Amplitude');
title('Original signal 21BEC1308');
% DPCM Transmitter
for n=1:length(x)
    if n==1
        e(n)=x(n);
        eq(n)=round(e(n));
        xq(n)=eq(n);
    else
        e(n)=x(n)-xq(n-1);
        eq(n)= round(e(n));
        xq(n)= eq(n)+xq(n-1);
    end
end
% DPCM receiver
for n=1:length(x)
    if n==1
        xqr(n)=eq(n);
    else
        xqr(n)= eq(n)+xqr(n-1);
    end
end
subplot(2,1,2);
plot(t,xqr,'b');
xlabel('Time');
ylabel('Amplitude');
title('DPCM 21BEC1308');

%dm--------------------------

clc
clear all
close all

%reading input signal parameters
am=input('enter the maximum amplitude:')
fm=input('enter the maximum frequency component:')
fs=20*fm;
t=0:(1/fs):(2/fm);
x=am*cos(2*pi*fm*t);

%defining empty arrays
xs=0; %empty array to store staircase
y=0; %empty array to store output
del=2*pi*am*fm*(1/fs); %defining step size

%filling the array with modulated signal
for i=1:length(x)
    if xs(i)<=x(i)
        xs(i+1)=xs(i)+del;
        b=1;
    else
        xs(i+1)=xs(i)-del;
        b=0;
    end
    y=[y,b];
end

figure("Name","DM without S0 distortion")
plot(x,'color','b','LineWidth',1)
title('DM (no slope overload distortion) 21BEC1308')
xlabel('Time')
ylabel('amplitude')
hold on
stairs(xs,'color','r','LineWidth',1)
hold off
grid on

%plot delta modulated signal
figure("Name","Delta modulated signal")
stairs(y,'color','m','LineWidth',1)
title('delta modulated signal 21BEC1308')
xlabel('Time')
ylabel('Amplitude')
grid on

%applying slope overload distortion
del1=4*pi*am*fm*(1/fs);
%defining empty arrays
xs1=0; %empty array to store staircase

y1=0; %empty array to store output

%filling the array with modulated signal
for i=1:length(x)
    if xs1(i)<=x(i)
        xs1(i+1)=xs1(i)+del1; %increment if amplitude is less
        b1=1;
    else
        xs1(i+1)=xs1(i)-del1;
        b1=0;
    end
    y1=[y1,b1];
end

figure("Name","DM with S0 distortion")
plot(x,'color','b','LineWidth',1)
title("DM (slope overload distortion)21BEC1308")
xlabel("Time")
ylabel("Amplitude")
hold on
stairs(xs1,'color','r','LineWidth',1)
hold off
grid on

%ask---------------------

clc
clear all
close all
fs = 1000; %Sampling frequency
num_cycle = 10; %number of cycles
fc = 100;
fs = 1000; %Sampling frequency
num_cycle = 10; %number of cycles
fc = 100; %carrier frequency
t = 0:1/fs:num_cycle/fc;
N = 8; %number of bits
m = rand(1,N);
for i = 1:N
 if m(i)>0.5
 m(i)=1;
 else
 m(i)=0;
 end
end
m; %sequence of bits
high = 10*sin(2*pi*fc*t); %high amplitude signal
low = 5*sin(2*pi*fc*t); %low amplitude signal
subplot(2,1,1);1;N;
plot(t,high)
xlabel('t')
ylabel('Amplitude')
grid on
title('High amp signal 21BEC1308')
subplot(2,1,2);
plot(t,low)
xlabel('t')
ylabel('Amplitude')
grid on;
title('Low amp signal 21BEC1308')
figure(2)
subplot(2,1,1)
stem(m) %plotting binary sequence
xlabel('n')
ylabel('Amplitude')
grid on;
title('Binary sequence 21BEC1308')
ts =1;
t1 = 1:ts:(length(m))*ts;
subplot(2,1,2)
s = stairs(t1,m);
op = [];
for i=1:N
 if m(i) == 0
 op = [op,low];
 else
 op = [op,high];
 end
end
figure(3)
plot(op) %ASK modulated signal
xlabel('Frequency')
ylabel('Amplitude')
grid on;
title('ASK output 21BEC1308')
demod = [];
for n = 0:N-1
 k = op(((n*length(high))+1):((n+1)*length(high)));
 a = max(xcorr(k,high));
 if a > (max(xcorr(high,low))) %comparing with threshold
 demod = [demod,1];
 else
 demod = [demod,0];
 end
end
demod; %demodulated output
figure(4)
stem(demod)
xlabel('n')
ylabel('Amplitude')
grid on;
title('Demodulated signal 21BEC1308 ')

%fsk--------------------------

clc;
clear all;
close all;
fs=1000;
num_cyc=10;
fch=100;
fcl=50;
t=0:1/fs:num_cyc/fch;
N=8;
high = 10*sin(2*pi*fch*t);
low = 10*sin(2*pi*fcl*t);
subplot(2,1,1)
plot(t,high)
title('High frequency signal 21BEC1308');
xlabel('t');
ylabel('amplitude');
subplot(2,1,2)
plot(t,low)
title('Low frequency signal 21BEC1308');
xlabel('t');
ylabel('amplitude');
m=rand(1,N);
for i=1:N
 if m(i)>0.5
 m(i)=1;clc;
 else
 m(i)=0;
 end
 disp(m)
end
figure(2)
subplot(2,1,1)
stem(m) %plotting binary sequence
title('binary sequence 21BEC1308')
xlabel('n')
ylabel('amplitude')
ts =1;
t1 = 1:ts:(length(m))*ts;
subplot(2,1,2)
s = stairs(t1,m);
op = [];
for i=1:N
 if m(i) == 0
 op = [op,low];
 else
 op = [op,high];
 end
end
figure(3)
plot(op) 
title('ASK output 21BEC1308')
xlabel('frequency')
ylabel('amplitude')

%psk---------------------

clc;
clear all;
close all;
n=8;
x=[0 0 0 0 1 1 1 0];                                             %random bit generator
bp=.000001;                                                    % bit period
disp(' Binary information at Trans mitter :');
disp(x);
% representation of transmitting binary information as digital signal 
bit=[]; 
for n=1:1:length(x)
    if x(n)==1;
       se=ones(1,100);
    else 
        se=zeros(1,100);
    end
    bit=[bit se];
end
t1=bp/100:bp/100:100*length(x)*(bp/100);
subplot(4,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volts)');
xlabel(' time(sec)');
title('Binary data in the form of a digital signal');
%XXXXXXXXXXXXXXXXXXXXXXX Binary-PSK modulation XXXXXXXXXXXXXXXXXXXXXXXXXXX
A=5;                                          % Amplitude of carrier signal 
br=1/bp;                                                         % bit rate
f=br*2;                                                 % carrier frequency 
t2=bp/99:bp/99:bp;                 
ss=length(t2);
m=[];                                          
kl=[];
for i=1:1:length(x)
    wave=A*sin(2*pi*f*t2);
    kl=[kl wave];
end
for i=1:1:length(x)
    if (x(i)==1)
        y=A*sin(2*pi*f*t2);
    else
        y=A*sin(2*pi*f*t2+pi);   %-A*sin(2*pi*f*t)
    end
    m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(4,1,2);
plot(t3,kl);grid on;
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('Carrier Signal');
subplot(4,1,3);
plot(t3,m);grid on;
xlabel('time(sec)');
ylabel('amplitude(volt)');
title('PSK modulated wave coresponding to binary information at the transmitter');
%XXXXXXXXXXXXXXXXXXXX Binary PSK demodulation XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
mn=[];
for n=ss:ss:length(m)  
  t=bp/99:bp/99:bp;
  y=sin(2*pi*f*t);                                        % carrier siignal 
  mm=y.*m((n-(ss-1)):n);
  t4=bp/99:bp/99:bp;
  z=trapz(t4,mm);                                              % intregation 
  zz=round((2*z/bp));                                     
  if(zz>0)                                        % logic level = (A+A)/2=0 
                         %becouse A*sin(2*pi*f*t+pi) means -A*sin(2*pi*f*t)
    a=1;
  else
    a=0;
  end
  mn=[mn a];
end
disp(' Binary data at Reciver :');
disp(mn);
%Representation of binary information as digital signal which is acheived 
%after PSK demodulation 
bit=[];
for n=1:length(mn)
    if mn(n)==1;
       se=ones(1,100);
    else
        se=zeros(1,100);
    end
     bit=[bit se];
end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(4,1,4);
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('PSK demodulated wave corresponding to binary information at the receiver ');
%>>>>>>>>>>>>>>>>>>>>>>>>>> end of program >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%
Editor is loading...