why am i getting complex values with IFFT?

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);

 Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Mayo de 2023
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 comentarios

Sam Blake
Sam Blake el 4 de Mayo de 2023
thank you for responding! okay so i need to find another way to reconstruct my matrix so that it preserves the complex components? the way i have it set right now it just multiplies them by 1 or 0 based off of whether its in the range of frequencies i want. I am confused on how multiplying a 1 or 0 removes the conjugate symmetry? i dont really understand the last part that you are explaining either.
Sam Blake
Sam Blake el 4 de Mayo de 2023
I found another way and think im starting to get onto what you were elaborating at the end. I appreciate your time to look over my code!
time = data(:,1); % Time in seconds
L = length(time);
Okay so far, you have one time for each sample in the time domain .
f = (0:L-1) * Fs/L
That calculates based on the time domain . But it is not relevant to the frequencies associated with data in the frequency domain.
Suppose you have data sampled at 1 Hz, t = 0, 1, 2, 3, 4, 5, 6 -- length = 7. You calculate (0:6)* 1 Hz / length=7 to get 0/7, 1/7, 2/7, 3/7, 4/7, 5/7, 6/7 and you associate those frequencies with the fft data -- as if the fft data is arranged so the bins are 0 Hz, 1/7 Hz, 2/7 Hz, 3/7 Hz, 4/7 Hz, 5/7 Hz, 6/7 Hz.
But that is not how the fft bins would be arranged. 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
You can do frequency indexing, but you need to be doubling the frequency; this is due to Nyquist constraints, that you need 2*N points to resolve frequency of N. And you would calculate the frequency indices, but then you have to take their symmetric components to account to match the negative frequencies. If you select the coefficient for (for example 4/7 Hz) then you also need to select the coefficient for -4/7 Hz.
It can be easiest to use fftshift() on a mix of negative and positive frequency values, do the masking based on abs() of the frequencies, and then ifftshift() to move the mask into position for masking bins.
okay now that all makes a lot more sense. Im now using this simple for loop that i believe handles the positive and negative frequencies appropriately. I am at least getting real numbers now from the Ifft so that is good to see. thanks again
Accel_recon = fftdata
k = 13
for i = (k+2):(L-k)
Accel_recon(i) = 0+0i
end
Sam Blake
Sam Blake el 4 de Mayo de 2023
disregard the "+0i" on line 4, that was included in error
Paul
Paul el 5 de Mayo de 2023
Editada: Paul el 17 de Mayo de 2023
"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

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 4 de Mayo de 2023

Editada:

el 17 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by