Tuesday, March 2, 2010

fftshift y ifftshift en MATLAB

FUNCIONALIDAD DE fftshift e ifftshift en Matlab

>> x=[1 2 3 4 5 6 7];

y=fftshift(x);
y = 5 6 7 1 2 3 4

z=ifftshift(x);
z = 4 5 6 7 1 2 3

--------------------------
>> x=[1 2 3 4 5 6 7];
>> yz=fftshift(ifftshift(x));
yz = 1 2 3 4 5 6 7

>> zy=ifftshift(fftshift(x));
zy = 1 2 3 4 5 6 7
------------------------

USANDO FTOT

% HELP: This command passes a group of samples from the frequency domain to
% the time domain.
% EXAMPLE: x=ftot(X)
% The samples in X freq domain are passed to the time domain in x

function [vector]=ftot(vector)
vector = fftshift(ifft(ifftshift(vector)));
end
------------------------
%% DEMUESTRA VENTAJA DE USAR TTOF
t0=20;
fs=100;
t=-t0/2:1/fs:t0/2; %Eje de tiempo
f=-fs/2:1/t0:fs/2; %Eje de frecuencia
x=sin(2*pi*2*t);
X=fft(x);
subplot(3,1,1)
plot(f,abs(X),'r');grid on;title('Usando solo FFT, con espectro de frecuencia sin centrar en 2Hz');
N=length(t); % numero de muestras
subplot(3,1,2)
plot(f,abs(X)/N,'r');grid on;title('Usando solo FFT dividiendo sobre N');
X=ttof(x);
subplot(3,1,3)
plot(f,abs(X)/N,'r');grid on;title('Usando funcion ttof, con la frec correctamente centrada en 2Hz');


----------------------
FUNCION TTOF (cambia del dominio del tiempo al dominio de la frec mediante fft)

% HELP: This command passes a group of samples from the time domain to
% the frequency domain.
% EXAMPLE: X=ttof(x)
% The samples in X freq domain are passed to the time domain in x
% t =-5:1/20:5;
% x = sin(2*pi*1*t)
function [vector]=ttof(vector)
vector = fftshift(fft(ifftshift(vector)));
end

No comments: