Skip to main content
Tweeted twitter.com/StackSignals/status/1561820524661166081
edited tags
Link
jojeck
  • 11.2k
  • 6
  • 38
  • 75
Adding plot suggested by user in comment.
Source Link
hadim
  • 213
  • 1
  • 2
  • 6

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
    
    all_t.append(t)
    all_amp.append(fft_mag.max())
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")

As MackTuesday ask in comment, I plotted the signal and related FFT for signal length = 1000 and 1400.

signal length = 1000 signal length = 1400

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
    
    all_t.append(t)
    all_amp.append(fft_mag.max())
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
    
    all_t.append(t)
    all_amp.append(fft_mag.max())
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")

As MackTuesday ask in comment, I plotted the signal and related FFT for signal length = 1000 and 1400.

signal length = 1000 signal length = 1400

deleted 22 characters in body
Source Link
hadim
  • 213
  • 1
  • 2
  • 6

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
 
    fft_amp = fft_mag[fft_mag.argsort()][::-1][0]
    
    all_t.append(t)
    all_amp.append(fft_ampfft_mag.max())
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
 
    fft_amp = fft_mag[fft_mag.argsort()][::-1][0]
    
    all_t.append(t)
    all_amp.append(fft_amp)
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")

I don't understand why FFT return different maximum amplitude as the signal length increase. I would except that with a large signal length according to the frequency, detected amplitude will be very accurate.

Plot amplitude from FFT against signal length

From where this periodic signal is coming ?

Here is the related python code I used to generate the plot:

%matplotlib qt
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt

def get_fft(x, dt):
    n = len(x)
    fft_output = np.fft.rfft(x)
    rfreqs = np.fft.rfftfreq(n, d=dt)
    fft_mag = [np.sqrt(i.real ** 2 + i.imag ** 2) / n for i in fft_output]
    return np.array(fft_mag), np.array(rfreqs)

def build_signal(amp, freq, signal_len):
    
    f = freq
    A = amp
    dt = 1
    t = signal_len

    x = np.arange(0, t, dt)
    y = A * np.cos(2*np.pi*f*x)

    fft_mag, rfreqs = get_fft(y, dt)
    return x, y, fft_mag, rfreqs, amp, freq, signal_len

# Build sin wave from 1 to 5000 signal length with freq=1e-3Hz and amplitude=0.5
all_t = []
all_amp = []
for t in np.arange(1, 5000, 100):
    x, y, fft_mag, rfreqs, amp, freq, signal_len = build_signal(amp=0.5, freq=0.001, signal_len=t)
    
    all_t.append(t)
    all_amp.append(fft_mag.max())
    
# Plot amplitude from FFT against signal length
plt.figure()
plt.plot(all_t, all_amp, 'o-')
plt.xlabel("Signal length")
plt.ylabel("Amplitude from FFT (0.25 is excepted)")
Source Link
hadim
  • 213
  • 1
  • 2
  • 6
Loading