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:





No comments:

Post a Comment