why am i getting complex values with IFFT?

252 次查看(过去 30 天)
I have accelerometer data given in a csv file that is loaded as shown and ran through a fourier transform to analyze and filter. The Ifft function after filtering returns values with complex components still. I really have no idea why i am new to fourier analysis and am quite in the dark here.I have attached the code up to the inverse fourier, if anyone can spot a mistake id appreciate you so much. Thanks for your time in advance.
clear all
close all
clc
data = readmatrix("sensor3.csv","Range","160:578"); % Impulse Input Data
%data = readmatrix("test 1.csv","Range","4000:5742");
%171
time = data(:,1); % Time in seconds
gforce_z = data(:,4) - 1; % Vertical G-Forces, Removed Acceleration caused by gravity
accel = gforce_z .* -9.8067; % Convert G's to m/s^2
figure(1) % Unprocessed G-force vs time data.
plot(time,accel)
title("Noisy Acceleration vs Time")
xlabel("Time (s)")
ylabel("Acceleration m/s^2")
%% Applying noise filters
%fourier analysis
L = length(time);
%Fs2 = (length(time)/(time(end)-time(1)));
Fs = 1/mean(diff(time)); %same thing as above but computationally easier
f = (0:L-1) * Fs/L; %frequency vector 1xnnnn
%fast fourier transform
fftdata = fft(accel);
%normalize fft
fft_norm = (1/L) * fftdata;
%plot phase angle and amplitude
figure(2)
%subplot(1,2,1)
plot(f,abs(fft_norm),'*'),xlabel('Frequency (Hz)'),ylabel('Amplitude')
% subplot(1,2,2)
% plot(f,angle(fft_norm),'*'),xlabel('Frequency (Hz)'),ylabel('Phase Angle')
%fft data into table
ffttable = table(fft_norm.',f,abs(fft_norm.'),angle(fft_norm.'))
ffttable.Properties.VariableNames = {'FFT coeff','Frequency','Amplitude','Phase Angle'}
disp(ffttable)
%%
%deciding dominant frequencies to keep
indices1 = f(length(time)/2:end) > 94 %(high)range of frequencies aloud in reconstruction
indices2 = f(1:(length(time)/2)) < 7 %Low range
indices = [indices2' ; indices1'] %make sure this vector length is = to length of fft
Accel_recon = indices.*fftdata %multipying by logical array to zero out
%undesireable amplitudes
%reconstructing acceleration data
accel_smooth = ifft(Accel_recon);

采纳的回答

Walter Roberson
Walter Roberson 2023-5-4
indices1 = f(length(time)/2:end) > 94 %(high)range of frequencies aloud in reconstruction
indices2 = f(1:(length(time)/2)) < 7 %Low range
Those are not symmetric.
In order to get real-valued results from ifft(), the data that you are applying ifft() to must have conjugate symmetry: it must be of the form
[dc_component, some_coefficients, conj(fliplr(some_coefficients))]
for the case where the data is an odd number of samples.
In the case that the data is an even number of samples,
[dc_component, some_coefficients, real_value, conj(fliplr(some_coefficients))]
In this case, the first value of the second half of the vector must be real valued (no imaginary component)
Another way of looking at this is that if you were to rotate the array half way around, it would go
[coefficients_for_negative_frequencies, dc_offset, coefficients_for_positive_frequencies]
and the N'th entry before the dc_offset (e.g., negative 38.9 Hz) would have to be the conjugate of the N'th entry after the dc_offset (e.g., positive 38.9 Hz)
You are constructing your mask as if the second half of the array is for the high frequencies and the first half is for the low frequencies, but when you are working with fft data, the second half is for the negative frequencies. The low positive frequencies are the first quarter, the high positive frequencies are the second quarter, the high negative frequencies (reversed) are the third quarter, the low negative frequencies (reversed) are the fourth quarter
  6 个评论
Sam Blake
Sam Blake 2023-5-4
disregard the "+0i" on line 4, that was included in error
Paul
Paul 2023-5-5
编辑:Paul 2023-5-17
"The fft bins would be 0 Hz, 2/7 Hz, 4/7 Hz, 6/7 Hz, -6/7 Hz, -4/7 Hz, -2/7 Hz"
I don't believe that's correct. For N = 7, these bins would be
f = (0:6)/sym(7)-1*[0 0 0 0 1 1 1]
f = 
if not using fftshift, or
fshift = (-3:3)/sym(7)
fshift = 
if using fftshift.
"But that is not how the fft bins would be arranged."
Having the bins defined as
f = (0:6)/sym(7)
f = 
is fine. Those positive frequencies greater than 3/7 are perfectly acceptable and is exactly how the bins would typically be defined to plot the output of fft vs f.
It is true that fft point at 4/7 maps to -3/7 in the fftshifted space, and these frequencies are equivalent in the sense that
isAlways(exp(1j*2*sym(pi)*4/7) == exp(1j*2*sym(pi)*-3/7))
ans = logical
1

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by