Fourier transformation of a file is shifting the data to the left.

1 visualizzazione (ultimi 30 giorni)
Hello!
I'm trying to do somewhat automated fourier analysis on a signal using the code below:
f = readmatrix("1.csv")
l = length(f) % Length of signal
fs = 1500 % sampling frequency
ts = 1/fs % sampling time
t = (0:l-1)*ts % time vector
plot (1000*t(1:50),f(1:50))
xlabel('Miliseconds (mS)')
ylabel('Amplitude(V)')
title('Signal')
ff = fft(f)
p2 = abs(ff/l);
p1 = p2(1:l/2+1);
fhz = fs*(0:(l/2))/l;
plot(fhz,p1)
title('one sided')
xlabel('frequency (Hz)')
ylabel('Amplitude')
However, when i use readmatrix it shifts the data to the left and the peaks of my fourier transform will be in the wrong locations.
The file "1.csv" was just a generic sine curve i generated in matlab with a 50Hz frequency and an amplitude of 1, which this script could handle fine when i defined "f" as being the sine function itself. The issue only arose when i used any given kind of data file to define the function.
I was hoping someone may have encountered a similar issue before and happens to know an answer or possible solution.
Thanks in advance!
  3 Commenti
This aint too bad
This aint too bad il 4 Mag 2022
of course, it was done like so:
t = linspace(0,10,10000)
f = sin(2*pi*50*t)
writematrix(f,"1.csv")
Hope that helps!
Jonas
Jonas il 4 Mag 2022
without having matlab available right now, you created your t with a sampling frequency with 1000 samples per second, in the analysis code you assume 1500 Hz. If i am not mistaken, this will lead to a shift in your spectrum

Accedi per commentare.

Risposta accettata

Jonas
Jonas il 4 Mag 2022
Modificato: Jonas il 4 Mag 2022
without having matlab available right now, you created your t with a sampling frequency with 1000 samples per second, in the analysis code you assume 1500 Hz. If i am not mistaken, this will lead to a shift in your spectrum
edit: i just checked, my assumption was true. correct the sampling frequency either in the analysis code or in the the creation code such that they are euqual
another point:
if you use one sided spectrum, please multiply the values of the non 0 frequency values by 2 to get the correct values
and: the creation of your t vector is slightly inprecise since it does not create exactly the temporal spacing you were asking for, maybe use dot notation instead to ensure correct temporal spacing
fs=1000;
t = 0:1/fs:10-1/fs;
% or t = linspace(0,10-1/fs,fs*10);
f = 3+sin(2*pi*50*t); % adding some dc offset which is not multiplied by 2 later
subplot(1,2,1);
l = length(f); % Length of signal
ts = 1/fs; % sampling time
t = (0:l-1)*ts; % time vector
plot (1000*t(1:100),f(1:100))
xlabel('Miliseconds (ms)')
ylabel('Amplitude (V)')
title('Signal')
ff = fft(f);
p2 = abs(ff/l);
p1 = p2(1:l/2+1);
p1(2:end)=p1(2:end)*2; % double frequency except 0 Hz (offset or mean)
fhz = fs*(0:(l/2))/l;
subplot(1,2,2);
stem(fhz,p1,'.')
title('one sided')
xlabel('frequency (Hz)')
ylabel('Amplitude')

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by