Help filtering a signal using fft
Afficher commentaires plus anciens
Hey,
I'm trying to filter a signal with the use of the fft. I would like to keep the lower frequencies and cut the higher frequencies.
I have given the code below.
I assume that I'm wrong in defining my filter variable... Right now, my FFT has length ~8000, and I wanted to keep the first 300 samples of that fft, and cut off the rest.
clear all;
close all;
load('dataTP.mat');
L=length(dataTP.data);
%create data vector, the one I want to filter
for i=1:length(dataTP.data)
data(i)=sqrt((dataTP.data(i,1))^2+(dataTP.data(i,2))^2+(dataTP.data(i,3))^2);
end
data = data-mean(data);
%create filter for use in fftfilt
filter=zeros(1,length(data));
filter(1:300)=1;
filtered_data=fftfilt(filter, data);
%create fft to visualize in graph
data_fft=fft(data);
P2 = abs(data_fft/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = 200*(0:(L/2))/L;
%plot fft visualization
figure(1)
plot(f,P1)
%plot raw data
figure(2)
plot(data);
%plot filtered data vs raw data
figure(3)
plot(F)
hold on
plot(data, 'r')
hold off
My first attempt was made like this:
data_fft=fft(data);
filter=zeros(1,length(data));
filter(1:300)=1;
filtered_fft=data_fft.*filter;
filtered_data=ifft(filtered_fft);
but that didn't work too well either... Hope you can help!
I have attached the data File.
Thanks!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Get Started with Signal Processing Toolbox dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!