Sampling and Effect of Aliasing: Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

SAMPLING AND EFFECT OF ALIASING

Program:
clc; clf; clear; t=-10:0.01:10; time axis. T=4; fm=1/T; xt=cos(2*pi*fm*t); subplot(2,2,1); plot(t,xt); fs1=1.5*fm; under sampling. fs2=2*fm; frequency. fs3=8*fm; sampling. n1=-4:1:4; xn1=cos(2*pi*n1*fm/fs1); sampled signal. subplot(2,2,2); stem(n1,xn1); o/p in discrete form hold on; plot(n1,xn1); xn1. title(Discrete time signal with fs<2fm); graph. xlabel('time n'); ylabel('amplitude x(n)); n2=-5:1:5; axis. xn2=cos(2*pi*n2*fm/fs2); sampled signal. subplot(2,2,3); stem(n2,xn2); o/p in discrete form. hold on; plot(n2,xn2); xn2. title(Discrete time signal with fs=2fm); graph. % Clear loaded screen. % Clear figure window. % Clear % Decide the length of the % Define the time period. % Calculate the frequency. % Generate the analog signal. % Access the first window. % plot a stem graph for the xt. % Specify a frequency foe % Generate the critical sampled % Specify a frequency foe over % Decide the length of time axis. % Generate the under % Access the second window. % Plot the stem graph of % Hold the samples. % plot a stem graph for the % Legend the title of the % Legend the x-axis. % Legend the Y-axis. % Decide the length of time % Generate the critical % Access the third window. % Plot the stem graph of % Hold the samples. % plot a stem graph for the % Legend the title of the

n3=-20:1:20; axis. xn3=cos(2*pi*n3*fm/fs3); signal. subplot(2,2,4); stem(n3,xn3); o/p in discrete form. hold on; plot(n3,xn3); xn3. title(Discrete time signal with fs>2fm); graph.

% Decide the length of time % Generate the over sampled % Access the fourth window. % Plot the stem graph of % Hold the samples. % plot a stem graph for the % Legend the title of the

Output:

d is c r e t e t im e s ig n a l w it h fs < 2 fm 1 0 .5 0 -0 .5 -1 -1 0 am plitude x (n) -5 0 5 10 1 0 .5 0 -0 .5 -1 -4

0 2 4 t im e n d i s c r e t e t i m e s i g n a l w i t h f s d=i s2 cf m e t e t i m e s i n g a l w i t h f s > 2 f m r 1 1 0 .5 0 -0 .5 -1 -2 0

-2

0 .5 0 -0 .5 -1 -5

-1 0

10

20

LINEAR CONVOLUTION:
Program:

clc; % Clear loaded screen. clear all; %clear n=[0:1:3]; % generate 4 points, say, between 0 and 3. m=[0:1:3]; % generate 4 points, say, between 0 and 3. x=input ('Enter input sequence = ') % define the input sequence. h= input ('Enter imp sequence = ') % define the impulse response. q=[min(n)+min(m):max(n)+max(m)]; % generate the range of output sequence. y=conv(x,h) % convolve input and impulse response. subplot (2,2,1); % divide the graph into four windows & access the first window. stem (n,x); the input xlabel('time index n'); ylabel('amplitude'); title('INPUT SEQUENCE'); grid; subplot(2,2,2); stem(m,h); the impulse xlabel('time index n'); ylabel('amplitude'); title('IMPULSE SEQUENCE'); grid; subplot(2,2,3); stem(q,y); the discrete signal xlabel('time index n'); ylabel('amplitude'); title('CONVOLUTION'); grid; % plot a colored stem graph for discrete signal. % legend the x-axis. % legend the y axis. % legend the title. % simulate a graph sheet. % access the second window. % plot a colored stem graph for discrete signal. % legend the x-axis. % legend the y axis. % legend the title. % simulate a graph sheet. % access the third window. % plot a colored stem graph for % % % % legend the x-axis legend the y axis legend the title. simulate a graph sheet.

Output:
Enter input sequence = [ 1 2 3 4 ] x=1 2 3 4 Enter impulse sequence = [ 4 3 2 1 ] h=4 3 2 1 y = 4 11 20 30 20 11 4

CIRCULAR CONVOLUTION:
Program:
clc; % Clear loaded screen. clf; % Clear figure window. clear; % Clear x1=input(Enter the sequence x1=); % Enter the first sequence. x2= input(Enter the sequence x2=); % Enter the second sequence. N1=length(x1); % Retrieve the length of the sequence x1. N2=length(x2); % Retrieve the length of the sequence x2. if N1==N2 %Check for condition length x1=length x2. xk1=fft(x1,N1) % If condition is true, obtain the DFT of 1st sequence. xk2=fft(x2,N1) % If condition is true, obtain the DFT of 2st sequence. xk3=xk1.*xk2 % Multiply both the DFT sequences xk1 and xk2. xn=ifft(xk3,N2) % Obtain IDFT of xk3. elseif N1 > N2 % If the condition is not true & N1 > N2. L2=N1-N2; %Find the length of 0s to be padded with N2. x3=[x2 zeros(1,L2)]; %Pad extra 0s to match xk1 and xk2 length. xk1=fft(x1,N1) % Obtain DFT of the first sequence. xk2=fft(x3,N1) % Obtain the DFT of the padded sequence x3. xk3=xk1.*xk2 % Multiply both the DFT sequences xk1 and xk2. xn=ifft(xk3,N1) % Obtain IDFT of xk3. elseif N2 > N1 % If the condition is not true & N2 > N1. L3=N2-N1; % Find the length of 0s to be padded with N1. x4=[x1 zeros(1,L3)]; % Pad extra 0s to match xk1 and xk2 length. xk1=fft(x4,N1) % Obtain the DFT of the first sequence. xk2=fft(x2,N1) % Obtain the DFT of the padded sequence x3.

xk3=xk1.*xk2 xk1 and xk2. xn=ifft(xk3,N1) end n=0:N1-1; stem(n,xn) discrete form. xlabel('samples') ylabel('amplitude') title('circular convolution') grid;

% Multiply both the DFT sequences % Obtain IDFT of xk3. % End the loop. % Define the range of samples. % Plot the stem graph of o/p in % Legend the x-axis. % Legend the Y-axis. % Legend the title of the graph. % Simulate a graph sheet.

Output:
Enter the sequence x1=[ 1 2 3 4] Enter the sequence x2=[4 3 2 1] xk1 = Columns 1 through 3 10.0000 -2.0000 + 2.0000i -2.0000 Column 4 -2.0000 - 2.0000i xk2 = Columns 1 through 3 10.0000 2.0000 - 2.0000i 2.0000 Column 4 2.0000 + 2.0000i xk3 = 1.0e+002 * Columns 1 through 3 1.0000 0 + 0.0800i -0.0400 Column 4 0 - 0.0800i X(n) = 24 22 24 30

DISCRETE FOURIER TRANSFORM


Program:
clc; clear all; x=input(Enter the sequence:); N=length(x); sequence. n=0:1:N-1; k=0:1:N-1; wn=exp(-j*2*pi/N); kn=n*k; wnk=wn.^kn; Exponent. xk=x*wnk; subplot(2,2,1) four windows & access the stem(k,xk); DFT. mag=abs(xk); DFT. ang=angle(xk); DFT. subplot(2,2,2); windows & access the % Clear loaded screen. % Clear % Define the discrete sequence. % Find the length of the input % Generate time axis. % Generate the Frequency axis. % Generate Twiddle Factor. % Generate Twiddle Factor Exponent. % Generate Twiddle Factor with % Generate DFT sequence. % Divide the graph into first window. % Plot a stem graph of absolute values of % Generate the absolute value of % Generate the phase angle of % Divide the graph into four second window.

stem(k,mag); values of DFT. title(Magnitude Response); subplot(2,2,3); windows & access the stem(k,ang); of DFT. title(Phase Response); grid;

% Plot a stem graph of absolute % Legend the title of the graph. % Divide the graph into four 3rd window. % Plot a stem graph of phase angle % Legend the title of the graph. % Simulate a graph sheet.

Output:
Enter the sequence: [ 1 2 3 4 ] dft output Columns 1 through 3 10.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i Column 4 -2.0000 - 2.0000i

FAST FOURIER TRANSFORM


Program:
x=input(Enter the sequence:); L1=length(x); sequence. N=input(N=); sequence. % Define the discrete sequence. % Find the length of the discrete % Define the length of DFT

if L1==N a=fft(x,N) elseif L1>N disp(time domain aliasing) else L2=N-L1; x1=[x zeros(1,L2)]; match x and N lengths. a=fft(x1,N) extended sequence. end y=abs(a) value of DFT sequence. z=angle(a) sequence. subplot(2,2,1) windows & access the stem(y,r:) values of DFT. xlabel(Samples) ylabel(Magnitude) title(Magnitude spectrum) grid; subplot(2,2,2) stem(z,m-) values of DFT. xlabel(Samples) ylabel(Phase) title(Phase spectrum) grid; disp(output); disp(a);

% % % %

% Check whether L1 = N. If the condition is true find FFT. If the condition is not true & L1 > N. % Display the error message. Using else statement. Check if the condition N > L1, is true. % If yes, pad extra 0s to % Now calculate DFT for the % End the loop. % Display the absolute

% Display the angle value of DFT % Divide the graph into four first window. % Plot a stem graph of absolute % Legend the x axis. % Legend the y axis. % Legend the title of the graph. % Simulate a graph sheet. % Access the second window. % Plot a stem graph of angle % Legend the x axis. % Legend the y axis. % Legend the title of the graph. % Simulate a graph sheet.

Output:
Enter the sequence: [ 1 0 0 0 0 0 0 0 ]

N=8 Output 1 1

GENERATION OF SIGNALS
Program:

clc; % Clear loaded screen. clear all; % Clear N=10; %decide the length of the sequence to be operated% n=[-5:1:4]; %decide the length of time axis% % Generation of unit step signal% u=zeros(1,5),ones(1,5); % generate step signal% subplot(3,3,1) %divide the graph into 9 windows & access 1st window% stem(n,u,'m') %plot the stem graph for discrete sequence % xlabel('time index n') %legend the x axis% ylabel('Amplitude a') %legend the y axis% title('Unit signal') %title the graph% grid; % simulate a graph sheet% % Generation of unit impulse signal% i=[zeros(1,5),ones(1,1),zeros(1,4)]; % generate impulse signal% subplot(3,3,2) %divide the graph into 9 windows & access 2nd window% stem(n,i,'m') %plot the stem graph for discrete sequence % xlabel('time index n') %legend the x axis% ylabel('Amplitude a') %legend the y axis% title('Impulse signal') %title the graph% grid; % simulate a graph sheet% % Generation of unit ramp sequence% n=0:1,10) %decide the length of time axis% r=n; % generate ramp signal% subplot(3,3,3) %divide the graph into 9 windows & rd access 3 window% bar(n,r) %plot the stem graph for discrete sequence r3% xlabel('time index n') %legend the x axis% ylabel('Amplitude a') %legend the y axis% title('Ramp Signal') %title the graph% grid; % simulate a graph sheet% % Generation of sinusoidal signal% s=5*sin(0.2*pi*n); %perform sine function% subplot(3,3,4) %divide the graph into 9 windows th &access 4 window% stem(n,s,'m') %plot the stem graph for discrete sequence r4% xlabel('time index n') %legend the x axis% ylabel('Amplitude') %legend the y axis% title('Sinusoidal signal') %title the graph%

grid;

% simulate a graph sheet%

% Generation of exponential signal n=0:10 %decide the length of time axis% s= exp(0.3*n); %perform exponential function% subplot(3,3,5) %divide the graph into 9 windows &access 5th window% stem(n,s,'m') %plot the stem graph for discrete sequence r4% xlabel('time index n') %legend the x axis% ylabel('Amplitude') %legend the y axis% title('Exponential signal') %title the graph% grid; % simulate a graph sheet% % Representation of square wave% n=0:.001:20 %decide the length of time axis% s=5*square(2*pi*n); %perform square function% subplot(3,3,5) %divide the graph into 9 windows &access 6th window% stem(n,s,'m') %plot the stem graph for discrete sequence %

Output:

You might also like