Showing posts with label FINAL YEAR. Show all posts
Showing posts with label FINAL YEAR. Show all posts

Friday, 9 September 2016

DSP - Auto Correlation (Scilab Code with Plot)

Scilab Code:

//Auto Correlation of x(n) is given as:
//R(k) = summation of x(n)*x(n-k) for n = -INF to INF
//Position of origin in the given sequence doesn't matter,
//Its only taken to plot the given sequence.
 
clc; close; clear;
 
x = input("Enter the sequence: ");
x_origin = input("Enter the position of origin in the sequence: ");
 
//plot the given sequence
subplot(2,1, 1);
x_lower = -(x_origin - 1);
x_upper = length(x) - x_origin;
n = x_lower : x_upper;
plot2d3(n, x, style=color("red"));
xtitle("x(n)","n");
a = gca();
a.children.children(1).thickness = 2;
a.y_location = "origin";
 
//plot the auto correlation
subplot(2,1, 2);
L = length(x);
n = -L+1:L-1;   //Elsewhere it'll be 0
y = xcorr(x);
disp(y, "Auto Correlation = ");
plot2d3(n, y, style=color("blue"));
xtitle("Auto Correlation of x(n)","n");
 
//The above code can also be done as follows:
//[y, ind] = xcorr(h);
//plot2d3(ind, y);
//No need to define the range.
 
a = gca();
a.y_location = "origin";
a.children.children(1).thickness = 2;


Console I/O:




Plot:





DSP - Cross Correlation (Scilab Code with Plot)

Scilab Code:


//Cross-correlation of x(n) and h(n) is:
//R(k) = summation of x(n).h(n-k) for n = -INF to INF
//Positions of origin in given sequences don't matter,
//they're taken only for plotting the given sequences.

clc; clear; close;

x = input("Enter First Sequence: ");
h = input("Enter Second Sequence: ");
x_origin = input("Enter the origin position in first sequence: ");
h_origin = input("Enter the origin position in second sequence: ");

x_upper = length(x) - x_origin;
h_upper = length(h) - h_origin;
x_lower = -(x_origin - 1);
h_lower = -(h_origin - 1);

//Plot x(n)
subplot(3,1, 1);
n = x_lower : x_upper;
plot2d3(n, x, style=color("red"));
xtitle("x(n)","n");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;

//Plot h(n)
subplot(3,1, 2);
n = h_lower : h_upper;
plot2d3(n, h, style=color("green"));
xtitle("h(n)","n");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;

//Plot the cross correlation
subplot(3,1, 3);
[y, ind] = xcorr(x, h);
disp(y, "Cross correlation  = ");
plot2d3(ind, y, style=color("blue"));
a = gca();
a.y_location = "origin";
xtitle("Cross Correlation of x(n) and h(n)","n","R(k)");

a.children.children(1).thickness = 2;


Console I/O:






Plot:






DSP - Circular Convolution of Discrete Time Signals (Scilab Code with Plot)

Scilab Code:

//Circular Convolution of x(n) and h(n) is given by:
//Summation of x(n).h(m-n) from 0 to N-1
//where, N = length of sequences
//Both the sequences start from the origin
clc; close; clear;

x = input("Enter the first sequence: ");
h = input("Enter the second sequence: ");

//perform zero-padding to make lengths equal
length_diff = length(x) - length(h);
if length_diff>0 then
    h = [h, zeros(1:abs(length_diff) )];
else
    x = [x, zeros(1:abs(length_diff) )];
end

x_origin = 1;
h_origin = 1;
x_lower = -(x_origin - 1);     //lower range of x
h_lower = -(h_origin - 1);     //lower range of h
x_upper = length(x) - x_origin;     //upper range of x
h_upper = length(h) - h_origin;     //upper range of h

//Create the x_matrix and h_matrix
len = length(x);
for i = 1:len
    x_mat(i, 1) = x(i);
    j = i;
    for c = 1:len
        h_mat(j, i) = h(c);
        j = j + 1;
        if j == (len+1)
            j = j - len;
        end
    end
end

y = h_mat * x_mat;
y = y';

disp(x, "x = ");
disp(h, "h = ");
disp(x_mat, "x_mat = ");
disp(h_mat, "h_mat = ");
disp(y, "Circular Convolution = ");

subplot(3, 1, 1);
plot2d3(x_lower:x_upper, x, style=color("green"));
xtitle("First Sequence","n","x(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;

subplot(3, 1, 2);
plot2d3(h_lower:h_upper, h, style=color("red"));
xtitle("Second Sequence","n","h(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;

subplot(3, 1, 3);
n = 0:(len-1);
plot2d3(n, y, style=color("blue"));
xtitle("Circular Convolution: x(n) x h(n)","n","y(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;


Console I/O:




Plot:





Thursday, 8 September 2016

DSP - Linear Convolution of Discrete Time Signals (Scilab Code with Plot)

Code:



clc; clear; close;
 
x = input("Enter the first sequence: ");
h = input("Enter the second sequence: ");
x_origin = input("Enter the position of origin in first sequence: ");
h_origin = input("Enter the position of origin in second sequence: ");
 
x_lower = -(x_origin - 1);     //lower range of x
h_lower = -(h_origin - 1);     //lower range of h
x_upper = length(x) - x_origin;     //upper range of x
h_upper = length(h) - h_origin;     //upper range of h
n_lower = x_lower + h_lower;
n_upper = x_upper + h_upper;
n = n_lower :1: n_upper;
 
disp(x_lower, "x_lower = ");
disp(h_lower, "h_lower = ");
disp(x_upper, "x_upper = ");
disp(h_upper, "h_upper = ");
disp(n, "n = ");
disp(conv(x, h), "Convolution = ");
 
y = conv(x, h);
 
subplot(3, 1, 1);
plot2d3(x_lower:1:x_upper, x, style=color("green"));
xtitle("First Sequence","n","x(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;
 
subplot(3, 1, 2);
plot2d3(h_lower:1:h_upper, h, style=color("red"));
xtitle("Second Sequence","n","h(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;
 
subplot(3, 1, 3);
plot2d3(n, y, style=color("blue"));
xtitle("Linear Convolution: x(n)*h(n)","n","y(n)");
a = gca();
a.x_location = "origin";
a.y_location = "origin";
a.children.children(1).thickness = 2;


Console I/O: 





Plot: