Showing posts with label dsp. Show all posts
Showing posts with label dsp. Show all posts

MATLAB program to perform circular convolution of two signals

Program Code

%circular convolution
clc;
close all;
clear all;
x=input('Please enter the first sequence x[n] = ');
h=input('Please enter the second sequence h[n] = ');
L1=length(x);
L2=length(h);
Len=max(L1,L2);
x=[x zeros(1,Len-L1)];
h=[h zeros(1,Len-L2)];
for i=0:(Len-1)
    y(i+1)=0;
    for j=0:(Len-1)
        m=mod(i-j,Len);
        y(i+1)=y(i+1)+(x(j+1)*h(m+1));
    end
end
disp('Convoluted sequence of x[n] and h[n] is given below: ');
disp(y);
subplot(311);
stem(x);
title('First sequence x[n]');
grid on;
subplot(312);
stem(h);
title('Second sequence h[n]');
grid on;
subplot(313);
stem(y);
title('Convoluted sequence ');

grid on;


Example of Output
Please enter the first sequence x[n] = [1 3 2 1]
Please enter the second sequence h[n] = [2 1 2]
Convoluted sequence of x[n] and h[n] is given below: 
     7     9     9    10



________________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .

MATLAB program to find out the Fourier Transform of sinusoidal waveform

Program code
clc;
clear all;
close all;
t=0:0.001:1;
cwtstruct = cwtft((sin(2*3.14*1000*t)),'plot');


Output


________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .



MATLAB program to perform linear convolution of two signals ( without using MATLAB function)

Program Code
clc;
close all;
clear all;
xt=input('Please enter the input sequence in time domain x[n]= ');
lxt=length(xt);
ht=input('Please enter the impulse sequence h[n]= ');
lht=length(ht);
ext=[xt zeros(1,(lht-1))];
eht=[ht zeros(1,(lxt-1))];
xdft=fft(ext);
hdft=fft(eht);
freqm=xdft.*hdft;
yt=[ifft(freqm)];
display('The convoluted sequence is given below');
disp(yt);
subplot(311);
stem(xt);
xlabel('Time');
ylabel('Magnitude');
title('Input sequence x[n]');
subplot(312);
stem(xt);
xlabel('Time');
ylabel('Magnitude');
title('Input sequence 1');
subplot(313);
stem(yt);
xlabel('Time');
ylabel('Magnitude');
title('Convoluted sequence');


Example of Output
Please enter the input sequence in time domain x[n]= [1 2 3 4 5 6]
Please enter the impulse sequence h[n]= [1 2 3 4 5 6]
The convoluted sequence is given below
  Columns 1 through 5 

    1.0000    4.0000   10.0000   20.0000   35.0000

  Columns 6 through 10 

   56.0000   70.0000   76.0000   73.0000   60.0000

  Column 11 

   36.0000



Matlab program to find the linear convolution of two signals (using matlab functions)

Program Code
%linear convolution (using matlab functions)
clc;
close all;
clear all;
x1=input('Please enter the input sequence x[n]= ');
x2=input('Please enter the starting time index of x[n]= ');
h1=input('Please enter the impulse response h[n]= ');
h2=input('Please enter the starting time index of h[n]= ');
y=conv(x1,h1);
n=x2+h2:length(y)+x2+h2-1;
display('The convoluted sequence is given below:');
y
stem(n,y);
xlabel('Time');
ylabel('Amplitude');
title('Linear Convolution');



Example of Output
Please enter the input sequence x[n]= [4 3 1 2]
Please enter the starting time index of x[n]= -2
Please enter the impulse response h[n]= [1 4 3]
Please enter the starting time index of h[n]= -1
The convoluted sequence is given below:

y =


     4    19    25    15    11     6

_________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


Matlab program to generate logarithmic curve


Program code
clc;
close all;
clear all;
a=input('Please enter the multiplication factor');
t=0:0.001:1;
p=log(a*t);
plot(p);
xlabel('Time');
ylabel('Amplitude');
title('LOGARITHMIC CURVE');
grid on;



Example of output
Please enter the multiplication factor 6

_______________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


Matlab program for Trapezoidal waveform


Program Code
clc;
clear all;
close all;
t=0:.5:4;
y=trapmf(t,[.5 1 2 2.5]);
plot (t,y);
axis ([0 4 -2 2]);
xlabel ('Time');
ylabel ('Amplitude');
title ('Trapezoidal Waveform');
grid on;                                         




Output 



Explanation of Program Code

 clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

clear all;
It removes all variables from the workspace. This frees up system memory.

close all;
It deletes all figures whose handles are not hidden.

t=0:.5:4;
It results in      
t =[0        .5     1      1.5           2      2.5      3         3.5          4]
Here a vector t is created having starting element as 0.5 and last element as 4 and with an increment of 0.5 between the elements. Here this vector t is used as time axis(x axis) while plotting trapezoidal waveform.

y=trapmf(t,[0.5 1 2 2.5]);
It is used to create trapezoidal curve. Trapmf is trapezoidal-shaped built-in membership function of fuzzy logic toolbox.The trapezoidal curve is a function of a vector, t , and depends on 0.5, 1, 2 and  2.5as given by
f(t,0.5, 1, 2, 2.5)=
 max{min{(t-0.5)/(1-0.5),  1, (2.5-t)/(2.5-2)}  0}
=max{min{(t-0.5)/0.5,   1,   (2.5-t)/0.5}           0}
0.5 and 2.5 locate the "feet" of the trapezoid.
1 and 2 locate the "shoulders" of the trapezoid.
          
plot (t,y);
It plots all the lines defined by t versus y pairs.

axis ([0 4 -2 2]);
It sets the limits for the x- and y-axis of the current axes.
Here according to the above command x-axis begins at 0 and ends at 4. Here y-axis begins at -2 and ends at 2.

xlabel ('Time');
It labels the x-axis as 'Time'. Each axes graphics object can have one label for the x-, y-, and z-axis. The label appears beneath its respective axis in a two-dimensional plot

ylabel ('Amplitude');
It labels the y-axis as ' Amplitude. '

title ('Trapezoidal Waveform');
It outputs the phrase  'Trapezoidal Waveform' above the figure at the top.

grid on; 
It adds major grid lines to the current axes.
_______________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .

MATLAB program for Z-Transform of finite duration sequence

Program Code
%ztransform of finite duration sequence
clc;
close all;
clear all;
syms 'z';
disp('If you input a finite duration sequence x(n), we will give you its z-transform');
nf=input('Please input the initial value of n = ');
nl=input('Please input the final value of n = ');
x= input('Please input the sequence x(n)= ');
syms 'm';
syms 'y';
f(y,m)=(y*(z^(-m)));
disp('Z-transform of the input sequence is displayed below');
k=1;
for n=nf:1:nl
    answer(k)=(f((x(k)),n));
   k=k+1;
end
disp(sum(answer));



Example of Output
If you input a finite duration sequence x(n), we will give you its z-transform
Please input the initial value of n = 0
Please input the final value of n = 4
Please input the sequence x(n)= [1 0 3 -1 2]
Z-transform of the input sequence is displayed below

3/z^2 - 1/z^3 + 2/z^4 + 1


Explanation of the program code
clc;
It clears all input and output from the Command Window display giving clean screen. It removes items from workspace, freeing up system memory. After using clc, the scroll bar cannot be used to see the history of functions, but still the up arrow can be used to recall statements from the command history.

close all;
It deletes all figures whose handles are not hidden.

clear all;
It removes all variables from the workspace. This frees up system memory.

syms ‘z’;
This statement creates symbolic variable z. syms function is used to creates symbolic variable.

disp(‘If you input a finite duration sequence x(n), we will give you its Z-Transform.’);
This statement displays the sentence
If you input a finite duration sequence x(n), we will give you its Z-Transform.
disp() function displays its arguments.

nf=input(‘Please input the initial value of n ’);
This statement displays the sentence
Please input the initial value of n.
After displaying this sentence, it waits for the user to input the initial value of n using keyboard. The value entered by the user will be stored in nf.

nl=input(‘Please input the final value of n ’);
This statement displays the sentence
Please input the final value of n.
After displaying this sentence, it waits for the user to input the final value of n using keyboard. The value entered by the user will be stored in nl.

x=input(‘Please input the sequence x(n)= ’);
This statement displays the sentence
Please input the sequence x(n)=
After that it waits for the user to input all the elements of the sequence x(n) using keyboard.  The user should type the opening square bracket [  before entering the first element of x(n). After entering the [ the user should input all the elements of the sequence x(n) in the correct order. The user should press the spacebar key after typing each element of the sequence x(n), except the last element.  After typing the last element of the sequence x(n), the user should type the closing square bracket ]. All the elements of the sequence x(n) entered by the user will be stored in the array x, in the same order as they are entered by the user. The first element of x(n) will be stored as the first element of the array x, the second element of x(n) will be stored as the second element of the array x, and so on.

syms ‘m’;
This statement creates symbolic variable m.

syms ‘y’;
This statement creates symbolic variable y.

f(y,m)=(y*(z^(-m)));
This statement defines a function f. This function f defined here, can take two arguments. This function f outputs an expression of the form y*(z^(-m)) in terms of z, in which there is the numerical value of the first argument in the place of y and the numerical value of the second argument in the place of m. For example, if the function f takes 3 as the first argument and 4 as the second argument, we get the output as 3/z^4 . We know that z^(-4) is 1/z^4 . In this program, the same function f(y,m) defined in this statement, will be later used in a for loop, with x(k) as y and n as m to calculate the z-transform of each element of the input sequence x(n).

disp(‘Z-transform of the input sequence is displayed below’);
This statement displays the sentence
Z-transform of the input sequence is displayed below

k=1;
This statement assigns the value 1 to the variable k.

for n=nf:1:nl
answer (k)= (f((x(k)),n));
k=k+1;
end
This for loop calculates the z-transform of each element of the input sequence and stores each of those z-transforms as elements of the array answer. I am going to explain each statement in this for loop.

for n=nf:1:nl
The above mentioned for loop starts with this statement. The value of n will be same as the value of nf during the first iteration of this loop. The value of n is incremented by one for each successive iterations of the loop. The iterations of the loop continues till the value of n becomes equal to the value of nl. The final iteration of the loop takes place when the value of n becomes equal to the value of nl.
The syntax of for loop is
for variable=initial value:increment to/decrement from the initial value during each iteration:final value
statements in the loop
.
.
.
.
end

answer(k)=(f((x(k)),n));
The RHS of this statement calculates the z-transform of one element of the input sequence x using the function f(y,m) with y=k and m=n and stores the z-transform of each element of x(n) as the corresponding element of the array answer. During the first iteration of this for loop, k=1, x(k)=x(1) and n=nf. So during the first iteration of this loop, this statement calculates the z-transform of the first element of the input sequence and stores the result as the first element of the array answer. During the second iteration of this loop, this statement calculates the z-transform of the second element of the input sequence and stores the result as the second element of the array answer and so on till the value of n becomes equal to the value of nf.

k=k+1;
This statement increments the value of k by 1 during each iteration of the for loop.

end
This statement terminates this for loop.

disp(sum(answer));
The final line of output of this program is displayed on the execution of this statement. sum(answer); adds all the elements of the array answer to find their sum, and disp(sum(answer)); displays the sum thus obtained. In this program, the sum of all elements of the array answer is the z-transform of the input sequence.
___________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .

MATLAB Program for Uniform distribution

Program Code
%Uniform Distribution
clc;
close all;
clear all;
n=input('Input the end value of x = ');
a=input('Input the starting value of uniform distribution, a=');
b=input('Input the ending value of uniform distribution, b= ');
y=1/(b-a);
m=(a+b)/2;
s=((b-a)^2)/12;
disp('mean is ');m
disp('variance is ');s
disp('value of f(x) is ');y
u=[zeros(1,(a-1)) ones(1,(b-a)) zeros(1,(n-b))];
z=y*u;
stem(z);
axis([0 n 0 1]);



Example of Output
Input the end value of x = 10
Input the starting value of uniform distribution, a=2
Input the ending value of uniform distribution, b= 8
mean is 
m =
    5

variance is 
s =
    3

value of f(x) is 
y =
   0.1667

______________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


MATLAB Program for Random sequence generation

Program Code
%Random Sequence Generation
clc;
close all;
clear all;
n=input('Input the length of Random sequence ');
y=linspace(1,n,n);
disp(y);
x=0+sqrt(1)*randn(1,n);
disp(x);
plot(y,x);
axis([1 10 -1.5 3.5]);
title('Random Sequence');
xlabel('Discrete Frequency');
ylabel('Amplitude');
grid on;



Example of Output

Input the length of Random sequence   13
     1     2     3     4     5     6     7     8     9    10    11    12    13

  Columns 1 through 7 
   -0.4326   -1.6656    0.1253    0.2877   -1.1465    1.1909    1.1892

  Columns 8 through 13 
   -0.0376    0.3273    0.1746   -0.1867    0.7258   -0.5883



_________________________________


If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .

MATLAB program for normal distribution

Program Code
% Normal Distribution
clc;
close all;
clear all;
x=input('Enter the input sequence ');
m=input('Enter the value of mean ');
s=input('Enter the standard deviation ');
n=length(x);
a=-(x-m).^2;
b=a/(2*s^2);
c=1/(s*((2*pi)^(1/2)));
d=exp(b);
y=c*d;
t=0:n-1;
plot(t,y);
title('Normal Distribution');
grid on;




Example of Output
Enter the input sequence [125 135 145 155 165 175 185 195]
Enter the value of mean 165.5

Enter the standard deviation 15.26



_______________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .


MATLAB program for sampling rate conversion

Program Code
%sampling rate conversion
clc;
close all;
clear all;
N=input('Enter the length of the sequence N= ');
n=0:N-1;
f1=input('Enter the first frequency component of the signal f1= ');
f2=input('Enter the second frequency component of the signal f2= ');
x=sin(2*pi*f1*n)+sin(2*pi*f2*n);
L=input('Input the up sampling factor L= ');
x1=zeros(1,L*N);
n1=1:L*N;
a=1:L:L*N;
x1(a)=x;
M=input('Input the down sampling rate M= ');
x2=x1(1:M:(L*N));
n2=1:((L*N)/M);
subplot(311);
stem(n,x);
xlabel('discrete frequency');
ylabel('Amplitude');
title('input sequence');
grid on;
subplot(312);
stem(n1,x1);
xlabel('discrete frequency');
ylabel('Amplitude');
title('up sampled sequence');
grid on;
subplot(313);
stem(n2,x2);
xlabel('discrete frequency');
ylabel('Amplitude');
title('down sampled sequence');
grid on;



Example of Output
Enter the length of the sequence N= 100
Enter the first frequency component of the signal f1= 1000
Enter the second frequency component of the signal f2= 250
Input the up sampling factor L= 3
Input the down sampling rate M= 2



____________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .



MATLAB program for equiripple FIR filter

Program Code
%equiripple FIR filter
clc;
close all;
clear all;
Fs=1000;
Fp=input('Input the pass band frequency Fp= ');
Fst=input('Input the stop band frequency Fst= ');
Ap=input('Input the pass band attenuation Ap= ');
Ast=input('Input the stop band attenuation Ast= ');
d=fdesign.lowpass('Fp,Fst,Ap,Ast');
Hd=design(d,'equiripple');
fvtool(Hd);




Example of Output
Input the pass band frequency Fp= 16000
Input the stop band frequency Fst= 12000
Input the pass band attenuation Ap= 1
Input the stop band attenuation Ast= 50



__________________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .



MATLAB program for Ideal Low Pass Linear Phase Fir Filter

Program Code
clc;
close all;
clear all;
wc=input('Input the cut-off frequency in radians(less than pi)');
M=input('Input the length of ideal filter');
if wc>pi
    error('cut-off frequency should be less than pi')
    return
end
alpha=(M-1)/2;
n=0:1:(M-1);
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);
if nargout==0
    stem(n,hd);
    title('Impulse response of ideal low pass filter');
    xlabel('n');
    ylabel('hd(n)');
end





                      Example of Output
Input the cut-off frequency in radians(less than pi)    3
Input the length of ideal filter     50

ans =

  Columns 1 through 7 

   -0.0123    0.0133   -0.0141    0.0147   -0.0151    0.0152   -0.0149

  Columns 8 through 14 

    0.0143   -0.0134    0.0120   -0.0102    0.0079   -0.0050    0.0016

  Columns 15 through 21 

    0.0025   -0.0075    0.0134   -0.0207    0.0297   -0.0412    0.0569

  Columns 22 through 28 

   -0.0800    0.1194   -0.2074    0.6350    0.6350   -0.2074    0.1194

  Columns 29 through 35 

   -0.0800    0.0569   -0.0412    0.0297   -0.0207    0.0134   -0.0075

  Columns 36 through 42 

    0.0025    0.0016   -0.0050    0.0079   -0.0102    0.0120   -0.0134

  Columns 43 through 49 

    0.0143   -0.0149    0.0152   -0.0151    0.0147   -0.0141    0.0133

  Column 50 


   -0.0123

___________________________

If you find this program code useful, then please deposit 5 Indian Rupees in my bank account, as my fee. (Citizens of Pakistan cannot do this.).
I am the woman who wrote this program code.
My name: Anju K.
My bank account number: 30221619108
Bank: State Bank of India, Chakkarakkal branch, India.
IFSC code: SBIN0070728
SWIFT code: SBININBB
BIC code: SBININBB
My email: tc9749@gmail.com .