DSP Dip Manual
DSP Dip Manual
DSP Dip Manual
T he name MATLAB stands for matrix laboratory. It was invented in the late 1970s by Cleve Moler, then chairman of the computer science department at the University of New Mexico.
MATLAB has evolved over a period of years with input from many users. In university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for highproductivity research, development and analysis. MATLAB was first adopted by control design engineers, Little's specialty, but quickly spread to many other domains. It is now also used in education, in particular the teaching of linear algebra and numerical analysis, and is popular amongst scientists involved with image processing. MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Its wide range of commands, functions, and language constructs permit users to solve and analyze difficult computational problems from science and engineering without programming in a general purpose language. Typical uses include: 1. Math and computation 2. Algorithm development 3. Modeling, simulation and prototyping 4. Data analysis, exploration and visualization 5. Scientific and engineering graphics
6. Application development, including graphical user interface building REAL TIME APPLICATIONS 1. DATA SECURITY 2. IMAGE MANIPULATIONS 3. SIGNAL PROCESSING
%%%%%%%% -------------DAY 1---------%%%%%%%% %%%%%%Some basic programs on images%%%%%% >> zeros(3) ans =
0 0 0
0 0 0
0 0 0
>> a = [1 2 3] a=
>> b = [1,2,3] b= 1 2 3
>> rand(3,4) ans = 0.9501 0.4860 0.4565 0.4447 0.2311 0.8913 0.0185 0.6154 0.6068 0.7621 0.8214 0.7919 >> randint(3) ans = 1 1 0 0 1 1 0 1 0
>> randn(3,4)%%Displays +ve and -ve values ans = -0.4326 0.2877 1.1892 0.1746 -1.6656 -1.1465 -0.0376 -0.1867 0.1253 1.1909 0.3273 0.7258
>> A = [1 2 3;4 5 6; 7 8 9] A= 1 4 7 2 5 8 3 6 9
>> B = size(A) B= 3 3
>> C = length(A) C= 3 >> A = [11 12 13;14 15 16;17 18 19] A= 11 12 13 14 15 16 17 18 19 >> B = sum(A) B= 42 45 48 >> C = sum(B) C= 135
>> D = diag(A) D= 11 15 19
>> E = diag(A, -1) E= 14 18 >> F = det(A) F= 0 >> G = norm(A) G= 45.6601 >> H = inv(A) Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.387779e-017.
H=
1.0e+014 * 1.8765 -3.7530 1.8765 -3.7530 7.5060 -3.7530 1.8765 -3.7530 1.8765
>> J = A' J= 11 14 17 12 15 18 13 16 19 >> K = A/2 K= 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000
>> L = mod(A,4) L= 3 2 0 3 1 0
Day 2
%%%%%%%%%------Image Processing------%%%%%%%%%%%
% How to read an image A=imread (filename); %%%%%%%%%%%%%%%%%%%%%%%%%%% % To display the image imshow (A); imview(A); %%%%%%%%%%%%%%%%%%%%%%%%%%% X=imread(Path); imshow(X); % Add a constant to an image. I = imread('rice.png'); J = imadd(I,50); subplot(1,2,1), imshow(I) subplot(1,2,2), imshow(J)
OUTPUT:
% Divide by a constant of the rice image. I = imread('rice.png'); J = imdivide(I,2); subplot(1,2,1), imshow(I) subplot(1,2,2), imshow(J)
OUTPUT:
OUTPUT:
5000
4000
3000
2000
1000
%Entropy %Entropy is used to characterize the texture of the input image. I = imread('circuit.tif'); J = entropy(I) OUTPUT: J = 6.9439
%A high-contrast image with its histogram. I = imread('cameraman.tif'); figure; imshow(I); imhist(I,64); OUTPUT:
4000 3500 3000 2500 2000 1500 1000 500 0 0 50 100 150 200 250
OUTPUT:
% To display the r, g, b components of an color image a=imread('football.jpg'); subplot(2,2,1),imshow(a);title('rgb image'); b=a(:,:,1); subplot(2,2,2),imshow(b);title('r component image'); c=a(:,:,2); subplot (2,2,3),imshow(c);title('g component image'); d=a(:,:,3); subplot(2,2,4),imshow(d);title('b component image');
rgb image
r component image
g component image
b component image
d=im2bw(a);
subplot(2,2,4),imshow(d);title('binary image');
rgb image
indexed image
binary image
% To convert an indexed image to the gray scale image [a,map]=imread('trees.tif'); imshow(a,map); f=ind2gray(a,map);figure, subplot(2,2,2),imshow(f);title('gray scale image');
original image
rgb image
ycbcr image
indexed image
% To rotate an image 35 counterclockwise and use bilinear interpolation. I = imread('circuit.tif'); J = imrotate(I,35,'bilinear'); imshow(I) figure, imshow(J)
OUTPUT:
% Crop an image clc; close all; clear all; I = imread('circuit.tif'); I2 = imcrop(I,[75 68 130 112]); imview(I), imview(I2)
OUTPUT:
% To increases the size of the image I = imread('circuit.tif'); J = imresize(I,1.25); imshow(I) figure, imshow(J) OUTPUT:
restored image
% Create geometric transformation structure I = imread('cameraman.tif'); tform = maketform('affine',[1 0 0; .5 1 0; 0 0 1]); J = imtransform(I,tform); imshow(I), figure, imshow(J)
OUTPUT:
DAY 3
Image enhancemen % Add gaussian noise to an image I = imread('eight.tif'); J = imnoise(I, 'gaussian'); imshow(I); figure, imshow(J); OUTPUT:
% Add salt & pepper noise to an image I = imread('eight.tif'); J = imnoise(I,'salt & pepper',0.02); imshow(I) figure, imshow(J) OUTPUT:
% Filter the noisy image with an averaging filter and display the results. I = imread('eight.tif'); imshow(I) J = imnoise(I,'salt & pepper',0.02); figure, imshow(J)
OUTPUT:
% Median filters to filter the noisy image and display the results. I = imread('eight.tif'); imshow(I) J = imnoise(I,'salt & pepper',0.02);
OUTPUT:
% Filtering of images, either by correlation or convolution I = imread('coins.png'); h = ones(5,5) / 25; I2 = imfilter(I,h); imshow(I), title('Original Image');
OUTPUT:
Original Image
Filtered Image
Blurred Image
Sharpened Image
%To blur an image using a function psf. I = imread('peppers.png'); I = I(10+[1:256],222+[1:256],:); figure;imshow(I);title('Original Image'); LEN = 31; THETA = 11; PSF = fspecial('motion',LEN,THETA); Blurred = imfilter(I,PSF,'circular','conv'); figure; imshow(Blurred);title('Blurred Image');
OUTPUT:
Original Image Blurred Image
LEN = 31; THETA = 11; PSF = fspecial('motion',LEN,THETA); Blurred = imfilter(I,PSF,'circular','conv'); figure; imshow(Blurred);title('Blurred Image'); wnr1 = deconvwnr(Blurred,PSF); figure;imshow(wnr1); title('Restored, True PSF');
OUTPUT:
Original Image
Blurred Image
figure; imshow(I); title('Original Image'); PSF = fspecial('gaussian',11,5); Blurred = imfilter(I,PSF,'conv'); V = .02; BlurredNoisy = imnoise(Blurred,'gaussian',0,V); figure;imshow(BlurredNoisy);title('Blurred and Noisy Image'); NP = V*prod(size(I)); [reg1 LAGRA] = deconvreg(BlurredNoisy,PSF,NP); figure,imshow(reg1),title('Restored Image');
Original Image
Restored Image
Restoration algorithms % deblurring with the lucy richardson filter I = imread('football.jpg'); subplot(2,2,1),imshow(I);title('Original Image'); PSF = fspecial('gaussian',5,5); % Create a simulated blur in the image and add noise. Blurred = imfilter(I,PSF,'circular','conv'); V = .02;
BlurredNoisy = imnoise(Blurred,'gaussian',0,V); subplot(2,2,2),imshow(BlurredNoisy);title('Blurred and Noisy '); luc1 = deconvlucy(BlurredNoisy,PSF,5); subplot(2,2,3); imshow(luc1); title('Restored Image');
Original Image Blurred and Noisy
Restored Image
original image
dct image
I = imread('coins.png'); imshow(I);title('original image'); I = im2double(I); T = dctmtx(8); B = blkproc(I,[8 8],'P1*x*P2',T,T'); mask = [1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; B2 = blkproc(B,[8 8],'P1.*x',mask); I2 = blkproc(B2,[8 8],'P1*x*P2',T',T); imshow(I), figure, imshow(I2);title('dct compressed image'); OUTPUT:
% Un sharp contrast enhancement filter I = imread('cameraman.tif'); subplot(2,2,1); imshow(I); title('Original Image'); H = fspecial('unsharp'); sharpened = imfilter(I,H,'replicate'); subplot(2,2,4); imshow(sharpened); title('Sharpened Image'); OUTPUT:
Wavelets
subplot(3,2,2),imshow(mat2gray(ca));title('approximation coefficents image'); subplot(3,2,3),imshow(ch);title('horizontal coefficients image'); subplot(3,2,4),imshow(cv);title('vertical coefficients image'); subplot(3,2,5),imshow(cd);title('diagnol coefficients image'); c=idwt2(ca,ch,cv,cd,'haar'); subplot(3,2,6),imshow(mat2gray(c));title('reconstructed image');
original image approximation coefficents image
reconstructed image
DAY 6
segmentation % Edge-detection method that edge provides is the Sobel and Canny method I = imread('coins.png'); imshow(I) BW1 = edge(I,'sobel'); BW2 = edge(I,'canny');
OUTPUT:
% edge-detection method that edge provides is the prewitt and Canny method z= imread('cameraman.tif'); BW1 = edge(z,'prewitt'); BW2 = edge(z,'canny'); imshow(BW1);title('prewitt image'); figure, imshow(BW2);title('canny image'); OUTPUT:
prewitt image
canny image
Segmentation % applying the watershed transform a=imread('football.jpg'); imshow(a);title('original image'); b=watershed(a); figure,imshow(b);title('transformed image');
original image
transformed image
%bwmorph means Morphological operations on binary images % Morphological operations are create connectivity array, % reconstruction morphologically etc. %remove-removes interior pixels. BW = imread('circles.png'); imshow(BW); BW2 = bwmorph(BW,'remove'); figure, imshow(BW2);
OUTPUT:
%SKELETONIZATION %To reduce all objects in an image to lines, without changing the essential structure of the image is known as skeletonization. BW1 = imread('circbw.tif'); BW2 = bwmorph(BW1,'skel',Inf); imshow(BW1); figure, imshow(BW2)
OUTPUT:
%Matrix to grayscale image convertion %filter2- 2-D digital filter %fspecial- Create predefined 2-D filter %mat2gray- Convert matrix to grayscale image I = imread('rice.png'); J = filter2(fspecial('sobel'),I); K = mat2gray(J); imshow(I), figure, imshow(K)
OUTPUT:
%Reverse black and white in a binary image. %imcomplement- Complement image bw = imread('text.png'); bw2 = imcomplement(bw); subplot(1,2,1),imshow(bw) subplot(1,2,2),imshow(bw2) OUTPUT:
% To plot the border on the image. I=imread(cameraman.tif); imshow(I) BW = im2bw(I); imshow(BW) dim = size(BW)
col = round(dim(2)/2)-90; row = min(find(BW(:,col))) boundary = bwtraceboundary(BW,[row, col],'N'); imshow(I) hold on; plot(boundary(:,2),boundary(:,1),'g','LineWidth',10); OUTPUT:
%To rotate an image 35 counterclockwise and use bilinear interpolation. I = imread('circuit.tif'); J = imrotate(I,35,'bilinear'); imshow(I) figure, imshow(J) OUTPUT:
%creating a mask to an image. I = imread('cameraman.tif'); BW = imread('text.png'); mask = BW(1:256,1:256); f = inline('imadjust(x,[],[],0.3)'); I2 = roifilt2(I,mask,f); imshow (I2)
OUTPUT:
%Edgetapering original = imread('cameraman.tif'); PSF = fspecial('gaussian',60,10); edgesTapered = edgetaper(original,PSF); figure, imshow(original,[]); figure, imshow(edgesTapered,[]); OUTPUT:
Signal lab
The dft command uses a straightforward method to compute the discrete Fourier transform. Define a vector x and compute the DFT using the command X = dft(x)
The command idft uses a straightforward method to compute the inverse discrete Fourier transform. Define a vector X and compute the IDFT using the command x = idft(X)
the command fft which performs a Fast Fourier Transform of a sequence of numbers. To compute the FFT of a sequence x[n] which is stored in the vector x, use the command X = fft(x)
Used in this way, the command fft is interchangeable with the command dft. For more computational efficiency, the length of the vector x should be equal to an exponent of 2, that is 64, 128, 512, 1024, 2048, etc. The vector x can be padded with zeros to make it have an appropriate length. MATLAB does this automatically by using the following command where N is defined to be an exponent of 2: X = fft(x,N);
The longer the length of x, the finer the grid will be for the FFT. Due to a wrap around effect, only the first N/2 points of the FFT have any meaning.
The FFT can be used to approximate the Fourier transform of a continuous-time signal as shown in Section 6.6 of the textbook. A continuous-time signal x(t) is sampled with a period of T seconds, then the DFT is computed for the sampled signal. The resulting amplitude must be scaled and the corresponding frequency determined. An M-file that approximates the Fourier Transform of a sampled continuous-time signal is available from the ftp site and is given below: function [X,w] = contfft(x,T); [n,m] = size(x); if n<m, x = x'; end Xk = fft(x); N = length(x); n = 0:N-1; n(1) = eps; X = (1-exp(-j*2*pi*n/N))./(j*2*pi*n/N/T).*Xk.'; w = 2*pi*n/N/T; The input is the sampled continuous-time signal x and the sampling time T. The outputs are the Fourier transform stored in the vector X and the corresponding frequency vector w.
The step response y is calculated and plotted from the following commands: num = 2; den = [1 2]; t = 0:3/300:3; % for a time constant of 1/2
y = step(num,den,t); plot(t,y) For the impulse response, simply replace the word step with impulse. For the response to an arbitrary input stored in x, type y = lsim(num,den,x,t); plot(t,y)
Convolution Commands : conv deconv To perform discrete time convolution, x[n]*h[n], define the vectors x and h with elements in the sequences x[n] and h[n]. Then use the command y = conv(x,h) This command assumes that the first element in x and the first element in h correspond to n=0, so that the first element in the resulting output vector corresponds to n=0. If this is not the case, then the output vector will be computed correctly, but the index will have to be adjusted. For example, x = [1 1 1 1 1]; h = [0 1 2 3]; y = conv(x,h); yields y = [0 1 3 6 6 6 5 3]. If x is indexed as described above, then y[0] = 0, y[1] = 1, .... In general, total up the index of the first element in h and the index of the first element in x, this is the index of the first element in y. For example, if the first element in h corresponds to n = -2 and the first element in x corresponds to n = -3, then the first element in y corresponds to n = -5. Care must be taken when computing the convolution of infinite duration signals. If the vector x has
length q and the vector h has length r, then you must truncate the vector y to have length min(q,r). See the comments in Problem 3.7 of the textbook for additional information. The command conv can also be used to multiply polynomials: suppose that the coefficients of a(s) are given in the vector a and the coefficients of b(s) are given in the vector b, then the coefficients of the polynomial a(s)b(s) can be found as the elements of the vector defined by ab = conv(a,b). The command deconv is the inverse procedure to the convolution. In this text, it is used as a means of dividing polynomials. Given a(s) and b(s) with coefficients stored in a and b, then the coefficients of c(s) = b(s)/a(s) are found by using the command c = deconv(b,a).
FUNCTION TO CALCULATE CONVOLUTION OF TWO SEQUENCES. % INPUTS: x1 & x2 are two input sequences %OUTPUT: Y CONVOLUTION OF x1 & x2. %usage:[a]=convolve(x1,x2) at mat lab prompt. function y=convolve(x1,x2) x1=5; x2=6; m=length(x1); n=length(x2); k=m+n-1; x1=[x1 zeros(1,n-1)]; x2=[x2 zeros(1,m-1)]; y=zeros(1,k); for i=1:k for j=1:i if i==1 % length of first input sequence. %length of second input sequence. % length of convolved sequence. %append zeros for ease of computation. % append zeros for ease of computation. % initialisation of response of thwe system.
y(i)=x1(1)*x2(1); else y(i)=y(i)+x1(i-j+1)*x2(j); end end end subplot(2,2,1) stem(1:1:m,x1) title('first Input signal') xlabel('time') ylabel('Amplitude')
subplot(2,2,3) stem(1:1:k,y) title('Convolved signal') xlabel('time') ylabel('Amplitude') % To Plot the Frequency Response of the First order System clear all;
b=[1]; a=[1,-.8]; w=0:.01:2*pi; [h]=freqz(b,a,w); subplot(2,1,1),plot(w/pi,abs(h)); title('frquency response of first order systemh(n)=0.^nu(n)'); xlabel('Normalised frequency') ylabel('magnitude'); subplot(2,1,2); plot(w/pi,angle(h)); xlabel('normalised frequency'); ylabel('phase in Radians'); %To plot the frequency response of the system clear all; b=[1,0,.9]; a=[1,0,.4]; d=[1,-1]; f=[1,0,.4]; [h,ph]=freqz(b,a); [h1,ph1]=freqz(d,f); subplot(2,2,1); plot(ph/pi,abs(h)); grid; xlabel('Normalised frequency') ylabel('magnitude');
subplot(2,2,2); plot(ph1/pi,abs(h1)); grid; xlabel('Normalised frequency') ylabel('Magnitude'); subplot(2,2,3); plot(ph/pi,angle(h)); grid; xlabel('Normalised frequency') ylabel('phase in radians'); subplot(2,2,4); plot(ph1/pi,angle(h1)); grid; xlabel('Normalised frequency') ylabel('phase in Radians'); %UNIT STEP SEQUENCE
clc; clear all; N=input('Enter the length of unit step sequence:'); t=0:1:N-1; y=ones(1,N); subplot(2,1,1); stem(t,y,'k','filled'); axis([0 N 0 2]);
fs=input('Enter the Sampling Rate') ts=1/fs; n=25:25; nts=n*ts; dt= 0.00005; t=-.005:dt:.005; xa=exp(-1000*abs(t)); xn=exp(-1000*abs(nts)); subplot(3,1,1); plot(t*1000,xa); xlabel('----->t in msec'); ylabel('x(t)'); title('Continuous Time Signalx(t)'); subplot(3,1,2); stem(xn); xlabel('----->n'); ylabel('x(n)'); title('discreate Time SIgnal x(t)');
title('ts=0.1 msec');
%reconstruction of the signal from its sample xa=xn*sin(fs*ones(length(n),1)*t-nts*ones(1,length(t))); subplot(3,1,3); plot(t,xa); xlabel('----->t in msec'); ylabel('x(t)'); title('Reconstructed signalx(t)fromx(n)');
%FINDING REAL AND IMAGINARY PARTS OF COMPLEX NUMBER %inputs: comp_number------complex number %outputs re and img-----real and imaginary numbers respectively function[Re,img]=reimg(comp_number); comp_number=3+4i; N=length(comp_number); for i=1:N re(i)=abs(comp_number(i))*cos(angle(comp_number(i))); img(i)=abs(comp_number(i))*sin(angle(comp_number(i)));
end subplot(2,2,1) stem(1:1:N,comp_number); title('input signal'); xlabel('time'); ylabel('amplitude') subplot(2,2,2) stem(1:1:length(re),re); title('real part of signal') xlabel('time'); ylabel('amplitude') subplot(2,2,3) stem(1:1:length(img),img) title('imaginary part of signal') xlabel('time'); ylabel('amplitude') %CALCULATION OF POWER SPECTRAL DENSITY (PSD) function sx=psd(x); x=100; n=length(x); u=2*n-1; x=[x zeros(1,n-1)]; rx=zeros(1,1); %insert %extra zeros to compensate extra length % initialize auto corellation %length of the input sequence
% PLOTS
subplot(2,2,1); plot(x); title('inputsignal'); xlabel('Time'); ylabel('Amplitude'); subplot(2,2,2); plot(10*log10(abs(sx))); title('magnitude spectrum'); xlabel('Frequency');
ylabel('power in db'); subplot(2,2,3); plot(angle(sx)); title('phase spectrum'); xlabel('Frequency'); ylabel('phase'); % IMPULSE RESPONSE OF THE SYSTEM function y=outsys(x,h) x=2; h=10; m=length(x);% Length of excitation n=length(h);% Length of impulse response of system k=m+n-1;%length of response x=[x zeros(1,n-1)];% Append zeros for ease of computation h=[h zeros(1,m-1)];% Append zeros for ease of computation y=zeros(1,k);% Initialisation of response of the system %compute the output of the system for j=1:i if i==1 y(i)=x(1)*h(1); end end subplot(2,2,1) stem(1:1:m,x); xlabel('time'); %phase spectrum of psd
ylabel('ampltue'); subplot(2,2,2);% impulse response of the system stem(n,h); title('impulse response of system'); xlabel('time'); ylabel('ampltude'); subplot(2,2,3);%output of the system stem(k,y); title('response of system'); xlabel('time'); ylabel('ampltude');
%FUNCTION TO CALCULATE THE DFT OF A GIVEN SEQUENCE %INPUTS: x---Input sequence %OUTPUT: X---DFT of a given sequence
end %DFT of a given sequence X=wn*x'; subplot(2,2,1) stem(1:1:N,x) title('Input Signal') xlabel('Time') ylabel('Amplitude')
subplot(2,2,2) stem(10*log10(abs(X))) title('MAGNITUDE RESPONSE OF PSD') xlabel('Frequency') ylabel('Amplitude') subplot(2,2,3) stem(angle(X)) title('PHASE RESPONSE OF PSD') xlabel('Frequency') ylabel('Phase')
[h,th]=freqz(b,a,32); % Frequency response plot clf subplot(2,2,1); plot(th,abs(h)); title('maginitude Response'); subplot(2,2,2); plot(th,angle(h)); title('phase Response'); xlabel('Radians');
%POLE-ZERO PLOT
% Frequency response of a digital filter %y(n)=x(n)-x(n-1) %Filter Definition b=[.0013 .0064 .0128 .0128 .0064 .0013]; a=[1.0 -2.9754 3.8060 -2.5453 0.8811 -0.1254]; % Frequency response implimentation [h,th]=freqz(b,a,128); % Frequency response plot clf subplot(2,2,1); plot(th,abs(h)); title('maginitude Response'); subplot(2,2,2); plot(th,angle(h)); title('phase Response'); xlabel('Radians');
% IMPULSE RESPONSE CALCLATION AND PLOT x=[1 zeros(1,20)]; y=filter(b,a,x); subplot(2,2,3); stem(y); xlabel('seconds'); title('impulse response');
Communications
Program 1:
% Sample the signal 100 times per second, for 2 seconds. %ammod-Amplitude modulation. %ssbmod-Single sideband amplitude modulation. %fft-Discrete Fourier transform. %abs-Absolute value and complex magnitude.
Fs = 100;
% Modulate x using single- and double-sideband AM. ydouble = ammod(x,Fc,Fs); ysingle = ssbmod(x,Fc,Fs);
% Compute spectra of both modulated signals. zdouble = fft(ydouble); zdouble = abs(zdouble(1:length(zdouble)/2+1)); frqdouble = [0:length(zdouble)-1]*Fs/length(zdouble)/2; zsingle = fft(ysingle); zsingle = abs(zsingle(1:length(zsingle)/2+1)); frqsingle = [0:length(zsingle)-1]*Fs/length(zsingle)/2;
% Plot spectra of both modulated signals. figure; subplot(2,1,1); plot(frqdouble,zdouble); title('Spectrum of double-sideband signal'); subplot(2,1,2); plot(frqsingle,zsingle); title('Spectrum of single-sideband signal');
Program 2:
AM Demodulation
%butter- Design Butterworth IIR digital filter using the specifications in filter specification object. %ammod- Amplitude modulation %amdemod- Amplitude demodulation t = .01; Fc = 10000; Fs = 80000; t = [0:1/Fs:0.01]'; s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal figure(1) ;plot(s) ; title(Modulating signal) ; [num,den] = butter(10,Fc*2/Fs); % Lowpass filter figure(2) ;plot([num,den]) ; title(Carrier signal) ; y1 = ammod(s,Fc,Fs); % Modulate. figure(3) ;plot(y1) ; title(Modulated signal) ;
Program 3:
Freequency Modulation
%fmmod- Freequency Modulation t = .01; Fc = 10000; Fs = 80000; t = [0:1/Fs:0.01]'; s = sin(2*pi*300*t)+2*sin(2*pi*600*t); % Original signal figure(1) ;plot(s) ; title(Modulating signal) ; [num,den] = butter(10,Fc*2/Fs); % Lowpass filter figure(2) ;plot([num,den]) ; title(Carrier signal) ; dev=50; y1 = ammod(s,Fc,Fs,dev); % Modulate. figure(3) ;plot(y1) ; title(Modulated signal) ; .
Program 4:
%Phase Modulation % Prepare to sample a signal for two seconds, % at a rate of 100 samples per second. Fs = 100; % Sampling rate t = [0:2*Fs+1]'/Fs; % Time points for sampling % Create the signal, a sum of sinusoids. x = sin(2*pi*t) + sin(4*pi*t); figure(1) ;plot(x) ; Fc = 10; % Carrier frequency in modulation phasedev = pi/2; % Phase deviation for phase modulation y = pmmod(x,Fc,Fs,phasedev); % Modulate. figure(2) ;plot(y) ; title(Modulated signal); z = pmdemod(y,Fc,Fs,phasedev); % Demodulate. figure(3) ;plot(z) ; title(Demodulated signal);
Program 5:
%PSK Modulation and Demodulation. len = 10000; % Number of symbols M = 16; % Size of alphabet msg = randint(len,1,M); % Original signal figure(1);plot(msg); % Modulate using both PSK txpsk = pskmod(msg,M); figure(2); plot(rxpsk); title('PSK Modulation Plot') % Demodulate the received signals. recovpsk = pskdemod(rxpsk,M); figure(3); plot(recovpsk); title('PSK DeModulation Plot');
Program 6:
%Quantization %partition- To specify a partition in MATLAB %codebook- codebook tells the quantizer which common value to assign %quantiz-Produce quantization index and quantized output value %legend-Graph legend for lines and patches t = [0:.1:2*pi]; % Times at which to sample the sine function
sig = sin(t); % Original signal, a sine wave partition = [-1:.2:1]; % Length 11, to represent 12 intervals codebook = [-1.2:.2:1]; % Length 12, one entry for each interval [index,quants] = quantiz(sig,partition,codebook); % Quantize. plot(t,sig,'x',t,quants,'.') legend('Original signal','Quantized signal'); axis([-.2 7 -1.2 1.2]
%CHEBYCHEV TYPE-1 LOW PASS FILTER clc; clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb1ord(w1,w2,rp,rs,'s'); [b,a]=cheby1(n,rp,wn,'s'); w=0:.01:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h));
an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); title('magnitude of the system'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->'); title('phase of the system');
Enter the pass band ripple0. 2 Enter the stop band ripple45 Enter the pass band freq1300 Enter the stop band freq1500 Enter the Sampling Frequency10000
0.1
0.2
0.3 0.4 0.5 0.6 0.7 (a)Normalised Frequency---> phase of the system
0.8
0.9
4
Phase in radians-->
2 0 -2 -4
0.1
0.2
0.8
0.9
%CHEBYCHEV TYPE-1 HIGH PASS FILTER clc; clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs,'s'); [b,a]=cheby1(n,rp,wn,high,'s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); title('magnitude of the system'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->'); title('phase of the system');
Enter the pass band ripple0.3 Enter the stop band ripple60 Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency9000
0.1
0.2
0.3 0.4 0.5 0.6 0.7 (a)Normalised Frequency---> phase of the system
0.8
0.9
4
Phase in radians-->
2 0 -2 -4
0.1
0.2
0.8
0.9
clc; clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb1ord(w1,w2,rp,rs,'s');
wn=[w1,w2]; [b,a]=cheby1(n,rp,wn,bandpass','s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple0.4 Enter the stop band ripple35 Enter the pass band freq2000 Enter the stop band freq2500
0
Gain in db--->
-200
-400
-600
0.1
0.2
0.8
0.9
4
Phase in radians-->
2 0 -2 -4
0.1
0.2
0.8
0.9
clc; clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb1ord(w1,w2,rp,rs,'s'); wn=[w1,w2]; [b,a]=cheby1(n,rp,wn,stop','s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple0.25 Enter the stop band ripple40 Enter the pass band freq2500 Enter the stop band freq2750
50
Gain in db--->
-50
0.1
0.2
0.8
0.9
4
Phase in radians-->
2 0 -2 -4
0.1
0.2
0.8
0.9
clc; clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq');
fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb2ord(w1,w2,rp,rs,'s'); [b,a]=cheby2(n,rp,wn,'s'); w=0:.01:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple.0.35 Enter the stop band ripple35
Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency8000
0
Gain in db--->
-20
-40
-60
0.1
0.2
0.8
0.9
2
Phase in radians-->
1 0 -1 -2
0.1
0.2
0.8
0.9
rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb2ord(w1,w2,rp,rs,'s'); [b,a]=cheby2(n,rp,wn,'high','s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple0.25 Enter the stop band ripple40 Enter the pass band freq1400 Enter the stop band freq1800 Enter the Sampling Frequency7000
10
Gain in db--->
0.1
0.2
0.8
0.9
2
Phase in radians-->
1 0 -1 -2
0.1
0.2
0.8
0.9
clc;
clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb2ord(w1,w2,rp,rs,'s'); wn=[w1,w2]; [b,a]=cheby2(n,rp,wn,'high','s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple0.4 Enter the stop band ripple40 Enter the pass band freq1400 Enter the stop band freq2000 Enter the Sampling Frequency9000
50
Gain in db--->
-50
-100
0.1
0.2
0.8
0.9
2
Phase in radians-->
1 0 -1 -2
0.1
0.2
0.8
0.9
clc;
clear all; format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=cheb1ord(w1,w2,rp,rs,'s'); wn=[w1,w2]; [b,a]=cheby1(n,rp,wn,stop','s'); w=0:.01/pi:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
Enter the pass band ripple0.3 Enter the stop band ripple46 Enter the pass band freq1400 Enter the stop band freq2000 Enter the Sampling Frequency8000
5
Gain in db--->
0 -5 -10 -15
0.1
0.2
0.8
0.9
2
Phase in radians-->
1 0 -1 -2
0.1
0.2
0.8
0.9
format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=buttord(w1,w2,rp,rs,'s'); %[b,a]=zp2tf(z,p,k); [b,a]=butter(n,wn,'s'); w=0:.01:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an); ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->');
OUTPUT OF THE BUTTERWORTH LOW PASS FILTER Enter the pass band ripple.6 Enter the stop band ripple.9 Enter the pass band freq1200 Enter the stop band freq1800 Enter the Sampling Frequency4000
0
Gain in db--->
-2
-4
-6
0.1
0.2
0.8
0.9
Phase in radians-->
-0.5
-1
-1.5
0.1
0.2
0.8
0.9
clc; clear all; %format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n,wn]=buttord(w1,w2,rp,rs,'s'); %[z,p,k] = butter(n,Wn,'s') %[b,a] = butter(n,Wn) %[b,a]=butter(n,wn,'s'); [b,a] = butter(9,300/500,'high') w=0:.01:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an);
ylabel('Phase in radians-->'); xlabel('(b) Normalised Frequency--->'); OUTPUT OF THE BUTTERWORTH HIGH PASS FILTER
Enter the pass band ripple.6 Enter the stop band ripple.9 Enter the pass band freq1200 Enter the stop band freq1800 Enter the Sampling Frequency4000
b=
Columns 1 through 7
0.0011 -0.0096
0.0384 -0.0895
0.1342 -0.1342
0.0895
Columns 8 through 10
-0.0384
0.0096 -0.0011
a=
Columns 1 through 7
1.0000
1.7916
2.5319
2.1182
1.3708
0.6090
0.1993
Columns 8 through 10
0.0431
0.0058
0.0004
20
Gain in db--->
0.1
0.2
0.8
0.9
4
Phase in radians-->
2 0 -2 -4
0.1
0.2
0.8
0.9
clear all; %format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); wp=input('Enter the pass band freq'); ws=input('Enter the stop band freq'); fs=input('Enter the Sampling Frequency'); w1=2*wp/fs; w2=2*ws/fs; [n]=buttord(w1,w2,rp,rs,'s'); wn=[w1,w2]; %[z,p,k] = butter(n,Wn,'s') %[b,a] = butter(n,Wn) %[b,a]=butter(n,wn,'s'); [b,a] = butter(n,wn,'bandpass','s'); w=0:.01:pi; [h,om]=freqs(b,a,w); m=20*log10(abs(h)); an=angle(h); subplot(2,1,1); plot(om/pi,m); ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); subplot(2,1,2); plot(om/pi,an);
Enter the pass band ripple0.6 Enter the stop band ripple9 Enter the pass band freq100 Enter the stop band freq800 Enter the Sampling Frequency4000
0
Gain in db--->
0.1
0.2
0.8
0.9
2
Phase in radians-->
1 0 -1 -2
0.1
0.2
0.8
0.9
clc; clear all; %format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); fp=input('Enter the pass band freq'); fs=input('Enter the stop band freq'); f=input('Enter the Sampling Frequency'); wp=2*fp/f;ws=2*fs/f; num=-20*log10(sqrt(rp*rs))-13; dem=14.6*(fs-fp)/f; n=ceil(num/dem); n1=n+1; if(rem(n,2)~=0) n1=n; n=n-1; end y=bartlett(n1); b=fir1(n,wp,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,1); plot(o/pi,m);
ylabel('Gain in db--->'); xlabel('(a)Normalised Frequency--->'); OUTPUT OF THE BARLETT LOW PASS FLTER
Enter the pass band ripple0.04 Enter the stop band ripple0.02 Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency8000
0
Gain in db--->
clc; clear all; %format long rp=input('Enter the pass band ripple');
rs=input('Enter the stop band ripple'); fp=input('Enter the pass band freq'); fs=input('Enter the stop band freq'); f=input('Enter the Sampling Frequency'); wp=2*fp/f;ws=2*fs/f; num=-20*log10(sqrt(rp*rs))-13; dem=14.6*(fs-fp)/f; n=ceil(num/dem); n1=n+1; if(rem(n,2)~=0) n1=n; n=n-1; end y=bartlett(n1); b=fir1(n,wp,high,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,2); plot(o/pi,m); ylabel('Gain in db--->'); xlabel('(b)Normalised Frequency--->'); OUTPUT OF THE BARLETT HIGH PASS FLTER
Enter the pass band ripple0.04 Enter the stop band ripple0.02
Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency8000
10
Gain in db--->
clc; clear all; %format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); fp=input('Enter the pass band freq'); fs=input('Enter the stop band freq'); f=input('Enter the Sampling Frequency'); wp=2*fp/f;ws=2*fs/f; num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f; n=ceil(num/dem); n1=n+1; if(rem(n,2)~=0) n1=n; n=n-1; end y=bartlett(n1); wn=[wp ws]; b=fir1(n,wn,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,3); plot(o/pi,m); ylabel('Gain in db--->'); xlabel('(c)Normalised Frequency--->'); OUTPUT OF THE BARLETT BAND PASS FLTER
Enter the pass band ripple0.04 Enter the stop band ripple0.02 Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency8000
10
Gain in db--->
clc; clear all; %format long rp=input('Enter the pass band ripple'); rs=input('Enter the stop band ripple'); fp=input('Enter the pass band freq'); fs=input('Enter the stop band freq'); f=input('Enter the Sampling Frequency'); wp=2*fp/f;ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13; dem=14.6*(fs-fp)/f; n=ceil(num/dem); n1=n+1; if(rem(n,2)~=0) n1=n; n=n-1; end y=bartlett(n1); wn=[wp ws]; b=fir1(n,wn,stop,y); [h,o]=freqz(b,1,256); m=20*log10(abs(h)); subplot(2,2,4); plot(o/pi,m); ylabel('Gain in db--->'); xlabel('(d)Normalised Frequency--->'); OUTPUT OF THE BARLETT BAND STOP FLTER
Enter the pass band ripple0.04 Enter the stop band ripple0.02 Enter the pass band freq1500 Enter the stop band freq2000 Enter the Sampling Frequency8000
2
Gain in db--->
OUTPUT:
50
Magnitude (dB)
-50
-100
0.1
0.2
0.9
1000
Phase (degrees)
-1000
-2000
0.1
0.2
0.9
%fir1-Window-based finite impulse response filter design load chirp % Load y and fs.
b = fir1(34,0.48,'high',chebwin(35,30)); freqz(b,1,512)
OUTPUT:
50
Magnitude (dB)
0.1
0.2
0.9
1000
Phase (degrees)
-1000
-2000
0.1
0.2
0.9
%Filter a signal using a filter with various initial conditions (IC) or no initial conditions. x = randn(100,1); b = fir1(50,.4); hd = dfilt.dffir(b); % Original signal. % 50th-order linear-phase FIR filter. % Direct-form FIR implementation.
y1 = filter(hd,x); zf = hd.states;
%Now use nonzero initial conditions by setting ICs after before you filter. hd.persistentmemory = true; hd.states = 1; y2 = filter(hd,x); stem([y1 y2]) % Different sequences at the beginning. % Uses scalar expansion.
OUTPUT:
1.5
0.5
-0.5
-1
-1.5
10
20
30
40
50
60
70
80
90
100