saving . . . saved
Weiner kinchin relation code
##### Question

Respected Sir/Madam I am developing code for weiner kinchin relation. I have hit a road block when the auto/cross correlation of a sequence would result in output length being almost double. How do I get over this problem. Your help will be priceless. Yours sincerely T.V.Chandra Shekar

Scilab 06-11-17, 4:30 p.m. chandrashekar

```h = rand(1,100);
data = xcorr(h);
plot2d3(data)
maxData = max(data);
[i,j] = find(data == maxData)
data = data(j:\$);
figure;clf();plot2d3(data)```
Please elaborate you problem if above solution is not working.

08-11-17, 8:48 p.m. ashutumho

I will post the code for you. So that you could get to the crux of the problem I am facing. Here the length of the 'scorr' variable would be twice of the original length of initial sequence chosen. Your help will be priceless. I have chosen known periodic signal because it would help visualize better.

Yours sincerely
T.V.Chandra Shekar

```clc;
clear all;

sample_rate=1000;
N = 1024;
t = (0:N-1)*1/(sample_rate);
s=sin(2*%pi*50*t) + sin(2*%pi*100*t);
subplot(3,1,1);
plot(t,s);xgrid(color("green"));
xtitle('Sine Signal','t Axis','sin(t)');
y=fft(s);
f=sample_rate*(0:(N-1))/N;
n=size(f,'*')
subplot(3,1,2);
plot(f,abs(y(1:n)));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequency','|y| Magnitude');
scorr = xcorr(s,s);
//disp(length(scorr));
l = 0:length(scorr)-1;
subplot(3,1,3);
plot2d(l,scorr);
fftsc = fft(scorr);
//fab = fftsc.^2;
//subplot(3,1,3);
//plot2d(l,fab);```

08-11-17, 9:07 p.m. chandrashekar

Try this

```clc;
clear all;

sample_rate=1000;
N = 1024;
t = (0:N-1)*1/(sample_rate);
s=sin(2*%pi*50*t) + sin(2*%pi*100*t);
subplot(3,1,1);plot(t,s);xgrid(color("green"));
xtitle('Sine Signal','t Axis','sin(t)');
y=fft(s);
f=sample_rate*(0:(N-1))/N;
n=size(f,'*')
subplot(3,1,2);
plot(f,abs(y(1:n)));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequency','|y| Magnitude');

//scorr = xcorr(s,s);
//l = 0:length(scorr)-1;
//subplot(3,1,3);
//plot2d(l,scorr);
//
scorr = xcorr(s,s);
maxData = max(scorr);
[i,j] = find(scorr==maxData);
scorr = scorr(j:\$);
l = 0:length(scorr)-1;
subplot(3,1,3);
plot2d(l,scorr);```

08-11-17, 9:25 p.m. ashutumho

I have tried your piece of code. Most of it has worked great for me. I have new problem new. When I plot the power spectrum taking the fft of auto-correlation the two graphs coincide well at frequency, but when it comes to magnitude of power there is a huge difference. Please help in this regard. I am posting the code so that you could make the necessary changes . Your help will be priceless.

yours sincerely
T.V.Chandra Shekar

```//Program To Verify Weiner Kinchine Relation
clc;
clear all;

sample_rate=1000;
N = 1024;
//Generating 1024 eqully spaced points between 0 and 1023;
//Distance between the points is (1/1000);
t = (0:N-1)*1/(sample_rate);
s=sin(2*%pi*50*t) + sin(2*%pi*100*t);
subplot(3,1,1);
plot(t,s);xgrid(color("green"));
xtitle('Sine Signal','t Axis','sin(t)');
y=fft(s);

//Generating 1024 equally spaced points between 0 and 1000;
//Distance between the points is (1000/1024)
f=sample_rate*(0:(N-1))/N;
n=size(f,'*')
subplot(3,1,2);
plot(f,abs(y(1:n)));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequency','|y| Magnitude');

scorr = xcorr(s,s);
maxData = max(scorr);
[i,j] = find(scorr==maxData);
scorr = scorr(j:\$);
fscorr = fft(scorr);
subplot(3,1,3);
plot2d(f,(abs(fscorr).^2));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequenc','|y| Magnitude');```

09-11-17, 8:49 p.m. chandrashekar

Is this code working fine

```clc;
clear all;

sample_rate=1000;
N = 1024;
//Generating 1024 eqully spaced points between 0 and 1023;
//Distance between the points is (1/1000);
t = (0:N-1)*1/(sample_rate);
s=sin(2*%pi*50*t) + sin(2*%pi*100*t);
subplot(3,1,1);
plot(t,s);xgrid(color("green"));
xtitle('Sine Signal','t Axis','sin(t)');
y=fft(s);

//Generating 1024 equally spaced points between 0 and 1000;
//Distance between the points is (1000/1024)
f=sample_rate*(0:(N-1))/N;
n=size(f,'*')
subplot(3,1,2);
plot(f,abs(y(1:n)));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequency','|y| Magnitude');

scorr = xcorr(s,s);
maxData = max(scorr);
[i,j] = find(scorr==maxData);
scorr = scorr(j:\$);
fscorr = fft(scorr);
subplot(3,1,3);
plot2d((sqrt(abs(fscorr))));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequenc','|y| Magnitude');```

12-11-17, 2:58 p.m. ashutumho

Yes it is working great. I am worried about the magnitude spectrum of cross correlation. Please give your valuable comment.

Yours sincerely
T. V. Chandra Shekar

12-11-17, 3:09 p.m. chandrashekar

Chandrashekar, Please look into this code

```clc;
clear all;

sample_rate=1000;
N = 1024;
//Generating 1024 eqully spaced points between 0 and 1023;
//Distance between the points is (1/1000);
t = (0:N-1)*1/(sample_rate);
s=sin(2*%pi*50*t) + sin(2*%pi*100*t);
subplot(3,1,1);
plot(t,s);xgrid(color("green"));
xtitle('Sine Signal','t Axis','sin(t)');
y=fft(s);

//Generating 1024 equally spaced points between 0 and 1000;
//Distance between the points is (1000/1024)
f=sample_rate*(0:(N-1))/N;
n=size(f,'*')
subplot(3,1,2);
plot(f,abs(y(1:n)));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequency','|y| Magnitude');

scorr = xcorr(s,s);
maxData = max(scorr);
[i,j] = find(scorr==maxData);
scorr = scorr(j-ceil(length(scorr))/4:j+ceil(length(scorr)/4));
fscorr = fft(scorr)//,-1,"nonsymmetric");
subplot(3,1,3);
plot2d(f,((abs(fscorr).^(1/2))));xgrid(color("green"));
xtitle('Magnitude Spectrum','f Frequenc','|y| Magnitude');```

12-11-17, 4:30 p.m. ashutumho