Вы находитесь на странице: 1из 25

http://deepeshrawat987.blogspot.

com/2015/03/

Matlab Code For Digital Modulation Techniques(ASK, FSK , PSK)

Digital Modulation Techniques (ASK, FSK And PSK)


%matlab code for digital modulation(ask, fsk and psk)
f=5;
f2=10;
x=[1 1 0 0 1 0 1 0] % input signal ;
nx=size(x,2);

i=1;
while i<nx+1
t = i:0.001:i+1;
if x(i)==1
ask=sin(2*pi*f*t);
fsk=sin(2*pi*f*t);
psk=sin(2*pi*f*t);
else
ask=0;
fsk=sin(2*pi*f2*t);
psk=sin(2*pi*f*t+pi);
end
subplot(3,1,1);
plot(t,ask);
hold on;
grid on;
axis([1 10 -1 1]);
title('Amplitude Shift Key')
subplot(3,1,2);
plot(t,fsk);
hold on;
grid on;
axis([1 10 -1 1]);
title('Frequency Shift Key')

subplot(3,1,3);
plot(t,psk);
hold on;
grid on;
axis([1 10 -1 1]);
title('Phase Shift Key')
i=i+1;
end

MAtlab Code for Square Wave Samping And Quantization

Square wave sampling and quantization

%%%%%%%%%% START %%%%%%%%%%%%

% program for sampling and quantization


clc
close all;
clear all;
t= [0:.1:2*pi];
s= square(2*t,50);
subplot(3,1,1)
plot(t,s)
title('Input Square signal')
subplot(3,1,2)
stem(t,s)
title('sampling of signal ')
v= [-1.8:.2:1]
w = [-2:.2:1]
[x,y] = quantiz(s,v,w);
subplot(3,1,3)
plot(t,s,t,y,'r')
legend('original signal ', 'quantization signal')
%%%%%%%%%% END %%%%%%%%%%%%

Matlab code for Pulse Code modulation


Pulse-code modulation (PCM) is a method used to digitally represent
sampled analog signals. It is the standard form of digital audio in
computers, Compact Discs, digital telephony and other digital audio
applications. In a PCM stream, the amplitude of the analog signal is sampled
regularly at uniform intervals, and each sample is quantized to the nearest value
within a range of digital steps.
PCM streams have two basic properties that determine their fidelity to the
original analog signal: the sampling rate, the number of times per second that
samples are taken; and the bit depth, which determines the number of possible
digital values that each sample can take.
http://en.wikipedia.org/wiki/Pulse-code_modulation

The matlab program given here is for finding pulse width modulation(PWM)
and pulse position modulation (PPM).

Pulse width modulation


Pulse position modulation

% Program for Pulse Code modulation with input signal x = [.2 .5 .3 .6]
clc
disp('select your choice 1 - pulse width 2 - pulse position')
i = input('select your choice - : ')
if i == 1

x = input('input signal = ')

[y,t ] = modulate(x,100,1000,'pwm','centered');
subplot(2,1,1)
plot(x)
title('Input signal')
subplot(2,1,2);
plot(t,y)
title('Pulse Width Modulation')
end
if i == 2

x = input('input signal = ')

[y,t ]= modulate(x,100,1000,'ppm',0.2);
subplot(2,1,1)
plot(x)
title('Input signal')
subplot(2,1,2);
plot(t,y)
title('Pulse Position Modulation')
end
MATLAB coding for Fourier series of rectangular pulse

Fourier series of rectangular pulse

%Fourier series of rectangular wave


clc;
close all;
clear all;
j=1;
T=4; %Time period of square wave
tau=1; %2tau= On time of the square wave
w0=2*pi/T;
N=50;
j=1;
for k=-N:1:N
if(k==0)
c(j)=2*tau/T; % fourier series coefficients of rectangular pulse
else
c(j)=2*sin(k*w0*tau)/(k*w0*T);
end
j=j+1;
end
k=-N:1:N;
subplot(2,1,1);
stem(k,c); %plot fourier series coefficients
grid on;
xlabel('k');
ylabel('fourier series coefficient of rectangular pulse');

%%----------------------------------------------------
l=1;
time=10;
for t=-time:0.01:time
sum=0;
j=1;
for k=-N:1:N
sum=sum+c(j)*exp(i*k*w0*t); %synthesis equation
j=j+1;
end
s(l)=sum;
l=l+1;
end
t=-time:0.01:time
subplot(2,1,2);
plot(t,s); % plot rectangular pulse
grid on;
xlabel('time');
ylabel('rectangular pulse');
MATLAB Code For SAMPLING THEORM

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
%%% sampling theorm %%%%%%%%%%%%%5
%%% x= sin(2*pi*f1*n)+cos(2*pi*f2*n) %%%%
%%% with f1= 2/128 & f2 = 4/128 %%%%%

clc;
close all;
clear all;
f1=2/128;
f2=4/128;
n=0:255;
fc=50/128;
x=sin(2*pi*f1*n)+cos(2*pi*f2*n);
xa=cos(2*pi*fc*n);
xamp=x.*xa;
subplot(2,2,1);plot(n,x);title('x(n)');
xlabel('n -->');
ylabel('amplitude');
subplot(2,2,2);
plot(n,xa);
title('xa(n)');
xlabel('n -->');ylabel('amplitude');
subplot(2,2,3);plot(n,xamp);
xlabel('n -->');ylabel('amplitude');

%%%%%%%%%%%%%%%%%%%%%%%%
MATLAB Code For Frequency Modulation
In FM, frequency of the carrier signal having high frequency is varied in
accordance with the instantaneous amplitude of the

modulating signal having low frequency. Frequency modulated signals are


widely used in television and radio transmission

systems. FM signals can be easily plotted using simple MATLAB functions.

%%%%% Program For Frequency modulation %%%%%%%%%%%%%


clc;
clear all;
close all;
fm=input('Enter the Message Frequency-: ');
fc=input('Enter the Carrier Frequency-: ');
mi=input('Modulation Index-: ');
t=0:0.001:1;
m=sin(2*pi*fm*t);% Message Signal
subplot(3,1,1);
plot(t,m);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
grid on;

c=sin(2*pi*fc*t);% Carrier Signal


subplot(3,1,2);
plot(t,c);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal');
grid on;
y=sin(2*pi*fc*t+(mi.*sin(2*pi*fm*t)));%Frequency modulation
subplot(3,1,3);
plot(t,y);
xlabel('Time');
ylabel('Amplitude');
title('FM Signal');
grid on;
%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%
Example
Enter the Message Frequency-: 10
Enter the Carrier Frequency-: 100
Modulation Index-: 5

Frequency modulation

MATLAB Code For Amplitude Modulation

Amplitude Modulation
AM is a method of transmitting signals, such as sound or digital information, in
which the amplitude of carrier wave is changed

according to the message signal. AM is widely used in electronic


communication field. The plotting of AM signal using MATLAB

is very easy.

%%%%%%%%%%% Amplitude Modulation %%%%%%%


clc;
clear all;
close all;
t=0:0.001:1;
set(0,'defaultlinelinewidth',2);
A=input('Enter the amplitude of message signal - : ');
fm=input('Enter the Message frequency - : ');% Message signal frequecny
fc=input('Enter the Carrier frequency - : ');% Carrier signal frequency
mi=input('Modulation Index - : ');

if (mi >1 || mi < 0 )


disp('Enter value between 0 and 1');
break;
end
x=A*sin(2*pi*fm*t);%Message Signal
subplot(3,1,1);%Plotting frame divided in to 3 rows and this fig appear at 1st
plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
grid on;

c=A*sin(2*pi*fc*t);%Carrier Signal
subplot(3,1,2);
plot(t,c);
xlabel('Time');
ylabel('Amplitude');
title('Carrier Signal');
grid on;

am=(A+mi*x).*sin(2*pi*fc*t);%AM Signal, Amplitude of Carrier changes to


(A+Message)
subplot(3,1,3);
plot(t,am);
xlabel('Time');
ylabel('Amplitude');
title('AM Signal');
grid on;

%%%%%%%%%%%% END %%%%%%%%%%%55

Enter the amplitude of message signal - : 2


Enter the Message frequency - : 5
Enter the Carrier frequency - : 100
Modulation Index - : 0.5

MATLAB CODE FOR CHANNEL ESTIMATION FOR OFDM SYSTEM

OFDM is simply defined as a form of multi-carrier modulation where the carrier


spacing is carefully selected so that each sub carrier is orthogonal to the other
sub carriers. Two signals are orthogonal if their dot product is zero That is, if
you take two signals multiply them together and if their integral over an interval
is zero, then two signals are orthogonal in that interval. OFDM is an optimal
version of multicarrier transmission schemes. For a large number of sub
channels, the arrays of sinusoidal generators and coherent demodulators
required in a parallel system become unreasonably expensive and complex. The
receiver needs precise phasing of the demodulating carriers and sampling times
in order to keep crosstalk between sub channels acceptable.
%CHANNEL ESTIMATION FOR OFDM SYSTEM
clear all
close all
clc
Mt=2;Mr=2;%the number of transmit and received antennas
N=64; %the number of subcarrier
nos=200;%number of symbol
P=3; %length of CP
M=N+P; %the totle length of an OFDM symbol
J=2; %collect J consecutively received OFDM symbols
L=3; %the order of channel
Nm=1;
h11=[-0.049+j*0.359 0.482-j*0.569 -0.556+j*0.587 1];
h12=[0.443-j*0.0364 1 0.921-j*0.194 0.189-j*0.208 ];
h21=[0.221-j*0.322 -0.199+j*0.918 1 0.284-j*0.524 ];
h22=[0.417+j*0.030 1 0.873+j*0.145 -0.285+j*0.309 ];
h1=[-0.049+j*0.359 0.443-j*0.0364;0.221-j*0.322 0.417+j*0.030];
h2=[0.482-j*0.569 1;-0.199+j*0.918 1];
h3=[-0.556+j*0.587 0.921-j*0.194;1 0.873+j*0.145];
h4=[1 0.189-j*0.208;0.284-j*0.524 -0.285+j*0.309 ];
H=[h1 h2 h3 h4];
%HH=[-0.049+j*0.359 0.221-j*0.322 0.482-j*0.569 -0.199+j*0.918 -
0.556+j*0.587 1 1 0.284-j*0.524 0.443-j*0.0364 0.417+j*0.030 1 1 0.921-
j*0.194 0.873+j*0.145 0.189-j*0.208 -0.285+j*0.309].';
HH=[h1.' h2.' h3.' h4.'].';
%%%%%%%%%%H=h(:);%
%H1
H1=zeros((J*M-L)*Mr,J*M*Mt);
H1(1:Mr,:)=[H zeros(Mr,(J*M-L-1)*Mt)];
for i=1:J*M-L-1
htmp=H1((i-1)*Mr+1:i*Mr,:);
H1(i*Mr+1:(i+1)*Mr,:)=[htmp(:,(J*M-1)*Mt+1:J*M*Mt),htmp(:,1:(J*M-
1)*Mt)];
end
%H1?
NRMSE=zeros(1,9);SNR=[];a=[];
SNR1=0:5:50;
forsnr=0:5:50
Herr=0;
for i1=1:Nm
Rr=zeros((J*M-L)*Mr,(J*M-L)*Mr);
%-----------------------------------------%
b0=round(rand(1,Mt*2*N*nos));
b=zeros(1,N*nos);
b1=reshape(b0,2,Mt*N*nos).';
b2=bi2de(b1,2,'left-msb');
b3=[1+j 1-j -1+j -1-j];
b=b3(b2+1)/sqrt(2);%1*12800;
%
d1=b(:);
for i=1:nos-J+1
d4(:,i)=d1((i-1)*N*Mt+1:(i+J-1)*N*Mt);
end
j=sqrt(-1);
W1=zeros(N,N);
for i1=1:N
for j1=1:N
W1(j1,i1)=exp(j*2*pi*(N-i1)*(j1-1)/N)/sqrt(N);
end
end
W=[W1,W1(:,1:P)].';
Ij=eye(J);%
Imt=eye(Mt);
W2=kron(Ij,W);
W3=kron(W2,Imt);%
W4=conj(W)*(W.');%
A=kron(Ij,W4);
s=W3*d4;
r1=H1*s;
n=rand(size(r1))+sqrt(-1)*rand(size(r1));
SNR=[SNR 20*log10(norm(r1))/norm(n)];
r=r1+n;%
Rr=r*r'/(nos-J+1); %?(J*M-L)*(J*M-L);
%
[U0,S0,V0]=svd(Rr);
%
Q=zeros((L+1)*Mr,(L+1)*Mr);
for k=J*N*Mt+1:(J*M-L)*Mr%
uk=U0(:,k);%256~262;
vk1=reshape(uk,Mr,J*M-L);%2*131?
vk=zeros((L+1)*Mr,J*M);%
vk(1:Mr,:)=[vk1 zeros(Mr,L)];%1*180?
for i3=1:L
vktmp=vk((i3-1)*Mr+1:i3*Mr,:);
vk(i3*Mr+1:(i3+1)*Mr,:)=[vktmp(:,J*M) vktmp(:,1:J*M-1)];
end
Q=Q+vk*A*vk';%vk;???8*8?
end
[U1 S1 V1]=svd(Q);
hr0=U1(:,(L+1)*Mr-Mt+1:(L+1)*Mr);
hb_h=mean(hr0./HH);
hb=hr0/hb_h;
HA=hr0(:,1)./hb_h(:,1);
HB=hr0(:,2)./hb_h(:,2);
hb=[HA HB];
Herr1=HH-hb;
for i4=1:Mt
Herr=Herr+(norm(Herr1(:,i4)))^2/(norm(H(:,i4)))^2;
end
end

figure(1)
te=length(H);
plot(1:te,real(H),'ko-',1:te,real(hb),'k+-')
legend('Estimated','Accurate')
figure(2)
plot(1:te,imag(H),'ko-',1:te,imag(hb),'k+-')
legend('Estimated','Accurate')
figure(3)
semilogy(SNR1,'-*')
xlabel('SNR/dB');ylabel('NRMSE');grid on;

%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%%%


MATLAB Code for OFDM Using Convolutional coding
This project is based on generation of OFDM using Convolutional coding.
Modulation technique used on this method is 16-QAM Modulation. Output of
this project is shown above also SNR is calculated. For MATLAB or '.m' file
contact me..
%%%%%%%%%%%%%%%%%%%%% START
%%%%%%%%%%%%%%%%%%%%%%

% OFDM Code
% No.of Carriers: 64
% coding used: Convolutional coding
% Single frame size: 96 bits
% Total no. of Frames: 100
% Modulation: 16-QAM
% No. of Pilots: 4
% Cylic Extension: 25%(16)

close all
clear all
clc
%%
% Generating and coding data
t_data=randint(9600,1)';
x=1;
si=1; %for BER rows

%%
for d=1:100
data=t_data(x:x+95);
x=x+96;
k=3;
n=6;
s1=size(data,2); % Size of input matrix
j=s1/k;

%%
% Convolutionally encoding data

constlen=7;
codegen = [171 133]; % Polynomial
trellis = poly2trellis(constlen, codegen);
codedata = convenc(data, trellis);

%%
%%BCH encoding of Data
h=fec.bchenc(7,4);
codedata1 = encode(h,data');
codedata1=codedata';

%%
%Interleaving coded data

s2=length(codedata);
j=s2/4;
matrix=reshape(codedata,j,4);

intlvddata = matintrlv(matrix',2,2)'; % Interleave.


intlvddata=intlvddata';
matrix1=reshape(codedata1,length(codedata1)/4,4);
intlvddata1 = matintrlv(matrix1',2,2)';
intlvddata1=intlvddata1';
%%
% Binary to decimal conversion

dec=bi2de(intlvddata','left-msb');
dec1=bi2de(intlvddata1','left-msb');

%%
%16-QAM Modulation

M=16;
y = qammod(dec,M);
y1 =qammod(dec1,M);

%%
% Pilot insertion

lendata=length(y);
lendata1=length(y1);
pilt=3+3j;
nofpits=4;

k=1;

for i=(1:13:52)
pilt_data1(i)=pilt;
for j=(i+1:i+12)
pilt_data1(j)=y(k);
pilt_data2(j)=y1(k);
k=k+1;
end
end

pilt_data1=pilt_data1';% size of pilt_data =52


pilt_data2=pilt_data2';
pilt_data(1:52)=pilt_data1(1:52); % upsizing to 64
pilt_data3(1:52)=pilt_data2(1:52);
pilt_data(13:64)=pilt_data1(1:52); % upsizing to 64
pilt_data3(13:64)=pilt_data2(1:52);

for i=1:52
pilt_data(i+6)=pilt_data1(i);
pilt_data3(i+6)=pilt_data2(i);
end

%%
% IFFT

ifft_sig=ifft(pilt_data',64);
ifft_sig1=ifft(pilt_data3',64);

%%
% Adding Cyclic Extension

cext_data=zeros(80,1);
cext_data(1:16)=ifft_sig(49:64);
cext_data1(1:16)=ifft_sig1(49:64);
for i=1:64

cext_data(i+16)=ifft_sig(i);
cext_data1(i+16)=ifft_sig1(i);
end

%%
% Channel

% SNR

o=1;
for snr=0:2:50

ofdm_sig=awgn(cext_data,snr,'measured'); % Adding white Gaussian Noise


ofdm_sig1=awgn(cext_data1,snr,'measured');

%%
% RECEIVER
%%
%Removing Cyclic Extension

for i=1:64

rxed_sig(i)=ofdm_sig(i+16);
rxed_sig1(i)=ofdm_sig1(i+16);

end

%%
% FFT

ff_sig=fft(rxed_sig,64);
ff_sig1=fft(rxed_sig1,64);

%%
% Pilot Synch%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:52

synched_sig1(i)=ff_sig(i+6);
synched_sig2(i)=ff_sig1(i+6);

end

k=1;

for i=(1:13:52)

for j=(i+1:i+12);
synched_sig(k)=synched_sig1(j);
synched_sig3(k)=synched_sig2(j);
k=k+1;
end
end

%%
% Demodulation
dem_data= qamdemod(synched_sig,16);
dem_data1=qamdemod(synched_sig3,16);

%%
% Decimal to binary conversion

bin=de2bi(dem_data','left-msb');
bin=bin';
bin1=de2bi(dem_data','left-msb');
bin1=bin1';
%%
% De-Interleaving

deintlvddata = matdeintrlv(bin,2,2); % De-Interleave


deintlvddata=deintlvddata';
deintlvddata=deintlvddata(:)';
deintlvddata1 = matdeintrlv(bin1,2,2);
deintlvddata1 = deintlvddata1';
deintlvddata1=deintlvddata1(:)';

%%
%Decoding data
n=6;
k=3;
decodedata =vitdec(deintlvddata,trellis,5,'trunc','hard'); % decoding datausing
veterbi decoder
rxed_data=decodedata;
%%
%Decoding of bch data
h1= fec.bchdec(h);
decodedata1 = decode(h1,deintlvddata1(1:189)');
decodedata1 = decodedata1';
rxed_data1=decodedata1;
%%
% Calculating BER
rxed_data=rxed_data(:)';
errors=0;
rxed_data1=rxed_data1(:)';
errors1=0;

c=xor(data,rxed_data);
c1=xor(data,rxed_data1(1:length(data)));
errors=nnz(c);
errors1=nnz(c);

BER(si,o)=errors/length(data);
BER1(si,o)=errors1/length(data);
papr(o)= max(abs(ofdm_sig).^2)/mean(abs(ofdm_sig).^2);
papr1(o)= max(abs(ofdm_sig1).^2)/mean(abs(ofdm_sig1).^2);
o=o+1;

end % SNR loop ends here


si=si+1;
end % main data loop

%%
% Time averaging for optimum results
for col=1:25; %%%change if SNR loop Changed
ber(1,col)=0;
for row=1:100;

ber(1,col)=ber(1,col)+BER(row,col);
end
end
ber=ber./100;
for col=1:25; %%%change if SNR loop Changed
ber1(1,col)=0;
for row=1:100;

ber1(1,col)=ber1(1,col)+BER1(row,col);
end
end
ber1=ber1./100;
%%

figure(1);
index=1:80;
pause(2)
plot(index,cext_data,'b',index,ofdm_sig,'r'); %plot both signals
legend('Original Signal to be Transmitted','Signal with AWGN');
title('In the Channel trilles encoded')
pause(2)
figure(2)
plot(index,cext_data1,'b',index,ofdm_sig1,'r'); %plot both signals
legend('Original Signal to be Transmitted','Signal with AWGN');
title('In the Channel BCH encoded')

pause(2)
figure(3)
i=0:2:48;
semilogy(i,ber);
title('BER vs SNR of trilles encoded signal');
ylabel('BER');
xlabel('SNR (dB)');
grid on
figure(4)
i=0:2:48;
semilogy(i,ber1);
title('BER vs SNR of bch encoded signal');
ylabel('BER');
xlabel('SNR (dB)');
grid on

%%%%%%%%%%%%%%%%%%%%%% END
%%%%%%%%%%%%%%%%%%%%%%%

Вам также может понравиться