Quickies (Matlab exercises 1 – 6)

%% exos periodo

%% exo1

clear all

n=5;

% Def of F

for j=0:n-1

for k=1:n

F(k,j+1)=exp(2*i*pi/n*j*k);

end

end

 

y=randn(n,1);

per1 = 1/n*(abs(F’*y)).^2;

figure, plot(per1)

 

hold on

per2=1/n*abs(fft(y)).^2;

plot(per2,’r.’)

 

% Remark:

% dftmtx(n)*y is the same as fft(y)

% but dftmtx(n) is not the same as F as defined in the lecture notes:

% the columns are the same exponentials

% but sampled one instant earlier for Matlab convention

% This changes the phase of the FT but not the modulus.

 

%% exo2 Dirichlet

sum(per1)-y’*y

sum(per2)-y’*y

 

%% exo3

n=100;

t=1:100;

y= 3*sin(2*pi*t/20+pi/3);

per = (1/n*abs(fft(y)).^2);

figure, plot(per)

 

%%

y2=[y zeros(1,1000)];

per2 = (1/n*abs(fft(y2)).^2);

figure, plot(per2,’r.’)

 

 

%% exo4

n=100;

t=1:100;

y= 3*sin(2*pi*t/18+pi/3);

per = (1/n*abs(fft(y)).^2);

figure, plot(per,’.’)

%% exo 5

close all

n=100;

t=1:100;

A=1/5;

y= A*sin(2*pi*t/20+pi/3)+randn(size(t));

per1 = (1/n*abs(fft(y)).^2);

figure, plot(per1)

 

n=1000;

t=1:n;

y= A*sin(2*pi*t/20+pi/3)+randn(size(t));

per2 = (1/n*abs(fft(y)).^2);

figure, plot(per2,’r’)

 

n=10000;

t=1:n;

y= A*sin(2*pi*t/20+pi/3)+randn(size(t));

per2 = (1/n*abs(fft(y)).^2);

figure, plot(per2,’g’)

 

%% exo 6

clear all

fs=1/(11*24*3600);

t=3*3600*24*(1:100);

n=length(t);

x=3*sin(2*pi*t*fs);

per2 = (1/n*abs(fft(x)).^2);

per2=per2(1:(length(t))/2+1);

freq=0:1/(t(end)-t(1)):(1/(t(end)-t(1)))*(length(per2)-1);

figure, plot(freq,per2)

%% Look at the behavior of a random process with different correlation coefficien

e=randn(1000000,1);

z(1)=0;

for i=2:length(e)

z(i)=e(i)+.99*z(i-1);

end

figure, plot(e)

hold on

plot(z,’r’)