FFT Frequency calculation wrong for non constant sample rate
1. Problem
FFT calculation with non constant sample rate is incorrect. The main frequency of the curve with nonconstant sample rate is 0.5Hz
2. Problem
FFT looks correct from the shape, but seems to be shifted
Labplot
Octave
Veröffentlichungsversion 2.9.0-1091-g699b4a469
Dec 12 2022, 04:32:00
System: Windows 10 Version 2009
Lokalisierung: German,Germany (Dezimalpunkt ',', Tausendertrenner '.'
Zahlenformat: Dezimalpunkt '.', Tausendertrenner ',', Exponentialzeichen 'e', Nullzeichen '0', Prozentzeichen '%', Positiv-/Negativzeichen '+'/'-' (Aktualisiert beim Neustart)
Architektur: x86_64-little_endian-llp64
Kernel: winnt 10.0.19043
C++ Compiler: MSVC 19.29.30038.1
C++ Compiler Optionen: /DWIN32 /D_WINDOWS /W3 /GR /EHsc /wd4250 /wd4251 /wd4396 /wd4661 /Zc:__cplusplus -O2 -W3 -DPSAPI_VERSION=1
Octave script to generate fft and signal
clc; clear; close all;
sample_rate = 1e-3;
x = 1:sample_rate:20;
frequency_signal = 5; % [Hz]
state = 1;
y = zeros(1, length(x));
for i = 1:length(x)
if mod(i, 0.5/(frequency_signal * sample_rate)) == 0
if state == 1
state = -1;
else
state = 1;
endif
endif
y(i) = state;
endfor
% y = sin(x * 2*pi*3); % just for testing. Peak at 3 Hz!
plot(x, y);
Nfft = length(x);
fvec = (-Nfft/2:Nfft/2 - 1 ) * 1/sample_rate/Nfft;
ytrans = fftshift(fft(y, Nfft));
figure;
plot(fvec, abs(ytrans));
csvwrite("ExportSignal.csv", [x', y']);
Edited by Martin Marmsoler