1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Compute FFT after reading accelerometer data

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 13:32.

  1. Stack

    Stack Membro Participativo

    I'd like to detect abnormal vibrations on a motor I have. I'm using the following devices:

    • Raspberry PI 5
    • Adafruit accelerometer ISM330DHCX

    I've successfully managed to read the sensor at a specific rate (2000Hz) and I'm using it's absolute value (although it's not changing that much):

    def read_sensor():
    global data
    target_duration = 1 / SAMPLE_RATE

    # Connect to sensor
    ...

    while True:
    loop_start_time = time.perf_counter()

    x, y, z = accelerometer.acceleration
    acceleration = np.sqrt(x**2 + y**2 + z**2)
    data = np.roll(data, -1)
    data[-1] = acceleration

    # Try to get exactly SAMPLE_RATE per second
    loop_duration = time.perf_counter() - loop_start_time
    sleep_time = max(0, target_duration - loop_duration)
    end_time = time.perf_counter() + sleep_time
    while time.perf_counter() < end_time: pass


    Then, I use numpy.fft to compute the FFT on the above data and save it to a CSV file:

    n = len(data)
    fft = abs(np.fft.fft(data * np.blackman(n)))[:n//2]
    amplitudes = np.abs(fft) / n

    # Identify max frequencies
    max_amplitude = np.max(amplitudes)
    max_index = np.argmax(amplitudes)
    frequencies = np.fft.fftfreq(n, d=1/n)[:n//2]

    # Generate summary
    result = {
    'mean': round(data.mean(), 4),
    'std': round(data.std(), 4),
    'max': round(data.max(), 4),
    'min': round(data.min(), 4),
    'range': round(data.max() - data.min(), 4),
    'fft_mean': round(np.mean(fft), 4),
    'fft_std': round(np.std(fft), 4),
    'max_freq': round(frequencies[max_index], 4),
    'max_amp': round(max_amplitude, 4),
    'speed': duty_cycle
    }


    It produces lines like these ones:

    mean,std,max,min,range,fft_mean,fft_std,max_freq,max_amp,speed
    9.9587,0.1261,10.599,9.5467,1.0523,15.6502,308.3268,0.0,4.1798,47
    9.9609,0.1287,10.599,9.5467,1.0523,16.2121,308.5345,0.0,4.1826,47


    I've attached a propeller to the motor and used a piece of soft plastic to hit the propeller. I known the Nyquist frequency theorem saying that I can only detect frequencies below SAMPLE_RATE/2 Hz.

    My questions are the following:

    • Why isn't the acceleration changing that much when I add the piece of plastic ?
    • Why is my FFT always showing 0.0 frequency ?
    • Where did I make a mistake in my code ?

    Continue reading...

Compartilhe esta Página