Skip to main content

Posts

Showing posts with the label Matlab Coding

Matlab Code to obtain DFT and plot its magnitude and phase response

Matlab Code:-   clc; clear all; close all; x=input('Enter the sequence: '); N=length(x); Wn=exp((-j*2*pi)/N);%Twiddle factor fprintf('Twiddle Factor=%f+j(%f)\n',real(Wn),imag(Wn)); disp('Magnitude and phase of Twiddle Factor:');     fprintf('|Wn|=%f    ',Wn);     fprintf('angle Wn=%f\n', angle(Wn));  fprintf('\n');  for k=1:N     X(k)=0; end %Calculation of DFT for k=1:N     for n=1:N         X(k)=X(k)+(x(n)*Wn^((k-1)*(n-1)));     end end %Printing the DFT fprintf('DFT of x(n) is:\n'); disp(X); fprintf('\n'); fprintf('X(k)\tMagnitude\tPhase\n\n'); for k=1:N     fprintf('X(%d)\t%f\t%f\n',k-1,X(k),angle(X(k))); end %To plot the magnitude and phase of the DFT subplot(1,2,1); stem(X,'linewidth',1.5); xlabel('k--->'); ylabel('magnitude--->'); title('Magnitude Response'); grid on; subplot(1,2,2); stem(angle(X),'linewidth',1.5); xlabel('k--->'); ylabel('...

Convolution of two signals using conv() in Matlab

We use the conv(x1,x2) function in Matlab to perform convolution of two discrete signals. For example, to perform the convolution of two discrete time sequences x1 (n) = {1, 0, 1,2}  and x2 (n) = {1, 2, 4, 6}. Code:  x1=[1,0,1,2]; %1st sequence N1=length(x1); %stores length of 1st sequence n1=0:1:N1-1; %range of x axis for 1st graph of 1st sequence subplot(1,3,1); % in a 1x3 grid, 1st sequence occupies the 1st grid stem(n1,x1,'linewidth',1.5); %to plot the sequence specifying time axis, signal and line width xlabel('n1----->'); %title of x axis ylabel('x1----->'); %title of y axis grid on; x2=[1,2,4,6]; %2nd sequence N2=length(x2);    %stores length of 2nd sequence n2=0:1:N2-1;  %range of x axis for 2nd graph of 2nd sequence subplot(1,3,2);  % in a 1x3 grid, 2nd sequence occupies the 2nd grid stem(n2,x2,'red','linewidth',1.5);  %to plot the sequence specifying time axis,signal,line width and colour xlabel('n2----->'); % title of...

How to plot a Continuous Time Sequence and Discrete Time Sequence in Matlab

1. For Continuous Time Sequence:        Use the function:  plot(X-axis, Y-axis)          For example to plot a parabolic function p(t)=  At^2 for t>=0                                                                                    0        for t<0           Code:           %UNIT PARABOLIC FUNCTION t=linspace(-20,20,20);      %range of X-asis A=1;                  %amplitude of the function for A=1 it is called unit parabolic function p=[t>=0].*(A.*t.^2)/2;      %parabola equation as defined above subplot(1,2,1);                ...