Matlab Programs

15
NATIONAL INSTITUTE OF TECHNOLOGY HAMIRPUR- (HP)-177005 MATLAB (2013) ( DIGITAL SIGNAL PROCESSING )

description

vry good programming you must see

Transcript of Matlab Programs

Page 1: Matlab Programs

NATIONAL INSTITUTE OF TECHNOLOGY

HAMIRPUR-(HP)-177005

MATLAB (2013)

( DIGITAL SIGNAL PROCESSING )

Submitted By:- Submitted To:-

Veerpratap singh Mr.Rakesh Sharma (12M419)

Page 2: Matlab Programs

%Program No. 1%

%Title: To calculate BER for various psk modulation techniques. %%Date:22 jan 2013

clear all;clc;x=randint(1,100000); %generating 10000 random samplesM=2;y = pskmod(x,M,pi/3); %psk modulation of generated random no. with pi/3 initial phase,BPSK or 2-arry psk%snr=1:1:20;

for i=1:1:20 Z=awgn(y,snr(i)); %awgn channel with i/p y % w =pskdemod(Z,M,pi/3); %psk demodulation with final phase pi/3 [number,ratio] = biterr(x,w); %calculating BER b/w rand. No. x & w channel o/p,giving number of bits got errored & ber% t(i)=ratio;end

semilogy(snr,t,'-xg')hold on;y = pskmod(x,8,pi/3); %8-arry psk,M=8%

for i=1:1:20 Z=awgn(y,snr(i)); w =pskdemod(Z,8,pi/3); [number,ratio] = biterr(x,w); t(i)=ratio;end

semilogy(snr,t,'-dr')hold on;

for i=1:1:20 Z=awgn(y,snr(i)); w =pskdemod(Z,16,pi/3); %16-arry psk,M=16% [number,ratio] = biterr(x,w); t(i)=ratio;endsemilogy(snr,t,'-db')xlabel('snr in db','fontsize',12);ylabel('ber','fontsize',12); title('plot bwn snr n ber');grid on;legend('bpsk','8psk','16psk')

Output: Figure 1:plot between SNR and BER for Bpsk,8-psk,16-psk

Page 3: Matlab Programs

Figure 2: BPSK Modulation & Demodulation on simulink.

Page 4: Matlab Programs

%Program No. 2%

%Title:To generate uniform(0,1) random variable by linear congruential method(LCM).%Date:05 FEB 2013

function [ u ] = lcm() %uniform random no. generation%

% Detailed explanation goes herem=input('enter modulus value:');b=log2(m);n=input('no. of samples to be generated');

%case 1%if mod(b,1)==0 %is modulus value m isin form of power of 2% c=input('enter increment value:'); if c~=0 g=gcd(m,c);%is m and increament value are relatively prime% if g==1 k=input('enter int_value to generate a:'); a=1+4*k %generate multiplier a value% seedValue=input('enter seed value:'); else disp('enter approperiate inc_Value') c=input('enter appro inc value') return end

%case 2% else seedValue=input('enter seed value:'); if mod(seedValue,2)~=0 %is seed value is odd% k=input('enter int_value to generate a:') a=3+8*k %generate multiplier a value% else disp('enter approperiate seedValue') return end end %case 3%else w=isprime(m); %is m is prime no.% c=input('enter the value of c:') if w==1 & c==0 k=m-1 a=input('enter the value of mul_a:') s=mod(a^k-1,m) %is (a^k-1)/m is a integer% if s==0 disp(' cas 3 is approached') seedValue=input('enter seed_val:') end else disp('enter appropriate modulus value:') return end

Page 5: Matlab Programs

%generate the random variable u according to the relation given below% end for i=1:1:n y=seedValue*a+c; x=mod(y,m); u(i)=x/m; seedValue=x; i=i+1; endend

OUTPUT:enter modulus value:32no. of samples to be generated:100enter increment value:1enter int_value to generate a:1

a =5

enter seed value:1

ans =

Columns 1 through 16

0.1875 0.9688 0.8750 0.4063 0.0625 0.3438 0.7500 0.7813 0.9375 0.7188 0.6250 0.1563 0.8125 0.0938 0.5000 0.5313

Columns 17 through 32

0.6875 0.4688 0.3750 0.9063 0.5625 0.8438 0.2500 0.2813 0.4375 0.2188 0.1250 0.6563 0.3125 0.5938 0 0.0313

Columns 33 through 48

0.1875 0.9688 0.8750 0.4063 0.0625 0.3438 0.7500 0.7813 0.9375 0.7188 0.6250 0.1563 0.8125 0.0938 0.5000 0.5313

Columns 49 through 64

0.6875 0.4688 0.3750 0.9063 0.5625 0.8438 0.2500 0.2813 0.4375 0.2188 0.1250 0.6563 0.3125 0.5938 0 0.0313

Columns 65 through 80

Page 6: Matlab Programs

0.1875 0.9688 0.8750 0.4063 0.0625 0.3438 0.7500 0.7813 0.9375 0.7188 0.6250 0.1563 0.8125 0.0938 0.5000 0.5313

Columns 81 through 96

0.6875 0.4688 0.3750 0.9063 0.5625 0.8438 0.2500 0.2813 0.4375 0.2188 0.1250 0.6563 0.3125 0.5938 0 0.0313

Columns 97 through 100

0.1875 0.9688 0.8750 0.4063

Figure 3:Emperical CDF of generated rand. No.

Page 7: Matlab Programs

%Program No. 3%

%Title:To generate Exponential Random variable using Inverse Transform%method.%Date:12 Feb 2013

clear all;clc;u=lcm();%uniform random no. generation%

p=input('enter the parameter lemda value:');

exp=-(1/p)*log(1-u); %exponential random no. gen. using inv.transform method%

y=hist(exp);n=input('enter again no. of samples to be generated:');e=y/n; %prob. Density fun.%subplot(2,2,1);cdfplot(exp)xlabel('x');ylabel('F(x),cdf of exp rand. var.');subplot(2,2,2);hist(exp)xlabel('x');ylabel('Histogram of exp. rand. variable');subplot(2,2,3);plot(0.1:1:10,e)xlabel('x');ylabel('f(x),pdf of exp. rand. var.');

OUTPUT:enter modulus value:64no. of samples to be generated:10000enter increment value:1enter int_value to generate a:1a =5enter seed value:-1enter the parameter lemda value:2

Page 8: Matlab Programs

Figure 4:empirical CDF,Histogram and PDF of exp. Random variable shown below:

Page 9: Matlab Programs

%Program No. 4%

%Title:To generate binomial data samples using inverse transform method.%Date:26 Feb 2013.

clc;clear all;clear figure;u = lcm();%uniform random no. generation%pi = input('enter the value of probability of success');n=input (' enter the parameter value of no. of trials');ip=(1-pi)^n; %initial value of prob. At i=0%

for i=0:n p(i+1)=((n-i)/(i+1))* ((pi/(1-pi)))*ip; %recursive formula of binom. R.V.% ip=p(i+1);endk=1;y=n;for j=1:length(u) if u(j)< p(1) x(j)=1; end pv=p(1); for k=2:n if pv<=u(j)&& u(j) < p(k)+pv x(j)=k+1; end pv=pv+p(k); end endsubplot(2,2,1); hist(x); xlabel('x');ylabel('Histogram of binomial. rand. variable'); subplot(2,2,2); cdfplot(x); xlabel('x');ylabel('F(x),cdf of Binomial rand. var.');

Page 10: Matlab Programs

Output: Figure 5: histogram of binomially distributed random variable & empirical CDF of same.

%Program No. 5%

Page 11: Matlab Programs

%Title:To generate absolute value 0f Z=N(0,1)has pdf%f(x)=(2/sqrt(2*pi))exp(-x^2/2);0<x<infinity using acceptance-rejection%method using known exp. distributed r.v.y with mean=1.%Date: 19 march 2013

clc;clear all;c=sqrt(2*exp(1)/pi) %max value of f(x)/g(x)%u=lcm();%uniform random no. generation%n=input('again Number of Data Samples to be generated ');y=-log(1-u); %exponentially gen. random no. %for k=1:n f(k)=u(k); if f(k)<= exp((-(y(k)-1)^2)/2) disp('accepted'); x(k)=y(k); else disp('rejected'); endendxz=hist(x);q=z/n;subplot(1,2,1);plot(q);subplot(1,2,2);cdfplot(x);

OUTPUT:Figure 6: Pdf of exp. Gen. rand. No. x & CDF of same.

%Program: 6:

Page 12: Matlab Programs

%Title:Chi-Square Test for Binomial Distribution,for uniformity or pdf of random variable.%Date:02 april 2013

clc;clear all;clf;

u=lcm(); %Function For uniform data generation%n=input('again How Many samples Want to Generate: '); r=input('Probability of Success');m=input('No of Trials');p(1)=(1-r)^m; %initial prob. At i=0 %f(1)=p(1);

for i=1:m p(i+1)=(factorial(m)*(r^i)*((1-r)^(m-i)))/((factorial(i)*factorial(m- i))); %recursive formula for binomially dist. Random no.%end for j=1:n for k=2:m+1 f(k)=f(k-1)+p(k); if u(j)<f(k-1) x(j)=k-2 %Binomial Random number break end end end v=hist(x,0:m); %histogram of binomially generated rand. No. x%

for q=1:m+1 e(q)=p(q)*n; %mean of bino.generated rand Oj(Ej=n*Pj)% d(q)=(v(q)-e(q))^2/e(q);end

T=sum(d) %Calculated Test statics for Chi square method%Z=input('enter the value of critical chi sqre value z(a/2): ');

if T<=abs(Z) disp('Ho:failure to reject the null hypotheshis')else disp('H1:reject the null hypotheshis')

end

OUTPUT: enter modulus value:128 no. of samples to be generated100 enter increment value:1 enter int_value to generate a:1,a =5

enter seed value:1

Page 13: Matlab Programs

again How Many samples Want to Generate: 100 Probability of Success:.2 No of Trials:15

T =1.9310

enter the value of critical chi sqre value z(a/2): 1.96

H0:failure to reject the null hypotheshis