# Vibrations All Around Us 7: Fourier Transform and Spectrogram

## Introduction

Welcome back to Vibrations All Around Us! A blog series investigating the Digital Signal Processing (DSP) algorithms used to detect, measure, and extract meaning from the vibrations happening around us. This is an important post as we will be introducing the fundamentals of Spectral analysis, also known as the Frequency Domain, and one of the most important algorithms in engineering: the Fast Fourier Transform (FFT).

## Frequency Vs. Time Domain

In the previous blogs, we looked at signal intensity (acceleration magnitude) with respect to the time that this signal intensity occurred at. It’s also possible for us to observe signal intensity with respect to the frequency that this signal intensity occurs at. To move from time to the frequency domain, we can use the Fast Fourier Transform. Let’s imagine a signal that’s composed of 2 cosines:

y(t) = 3cos(2\pi t * 10 - \pi /2) + 8cos(2\pi t * 4)

We can summarize the energies in the signal as the following:

1. A 10 Hz. energy with amplitude 3 and a phase of -90°
2. A 4  Hz. energy with amplitude 8 and a phase of 0°

Let’s use the Fast-Fourier Transform algorithm (FFT) to convert to the frequency domain using the time series of the signal. We can calculate 2 plots from the FFT, Amplitude Vs. Frequency and Phase Vs. Frequency. We can use FFT and python to generate the time series of the two-cosine equation outlined above, take the one-sided FFT of the time series signal, and plot the amplitude and phase of the frequency domain conversion.

We see that the FFT generates frequency domain plots which confirm our knowledge of the signal as we had outlined in our “energy summary”, the 2 dominant frequencies at 4 and 10 Hz with amplitudes of 8 and 3, respectively, and a phase of -90° and 0°, respectively. You’ll notice that there are non-zero amplitudes at the frequencies around 4 and 10 Hz. This is called “spectral leakage”, and can be expected when a non-integer number of periods are processed through the FFT.

You’re probably thinking “what’s the use of being able to express a couple of sinusoids in this fashion? In the real world, signals don’t take the form of nicely sculpted sinusoids.” The power of the FFT is that it can be scaled up to any number of sinusoids as long as your sampling frequency is twice the highest frequency you are analyzing! Fourier stipulated that any waveform could be expressed as an infinite number of sinusoids of varying amplitude, frequency, and phase – meaning we can generate these plots for any waveform. That means that the FFT can be applied to images to identify color frequencies, financial data to identify spending/cost seasonality, audio information to identify pitches, and accelerometer data to identify mechanical vibrations.

## Desk Fan Vibration Experiment

I’ve stuck the Nicla to the top of a small fan sitting on my desk. The fan blade that spins is going to have some small imbalance in it that will produce a centrifugal force on the housing, causing it and the Nicla’s accelerometer to vibrate at the frequency the fan blade is spinning at. I’ve taken 10 seconds of data while the fan is spinning. If we take the resultant of the accelerometer signal and plot the time series, we won’t be able to make much meaning out of the garbled mess, but if we instead take the FFT and plot magnitude vs frequency, we should see that the body of the fan shakes at the frequency that the fan is spinning at. Remember that sampling rate in the time domain defines how finely a signal is converted into discrete time intervals and sampling rate in the frequency domain affects the range of frequencies that can be accurately represented by the FFT meaning we will be limited to analyzing half our sampling rate (Nyquist).

This is not going to look as pretty as the FFT of y(t) that we just did, because of noise which is present at a large number of frequencies and because the mechanical vibrations of my \$20 desk fan are not going to vibrate at pure tones. Nonetheless, it’s obvious that there is a high energy signal at 45 Hz, indicating that the fan is probably spinning at this frequency. Let’s increase the speed of the fan by switching it from the “White Noise” to “Refresh” setting and create the same plot.

The fan’s speed has increased from 45 Hz to about 50 Hz. Not only does the fan’s speed (dominant frequency) increase, but we also see the dominant frequency has a larger amplitude. The amplitude has more than doubled from only a 5 Hz speed increase, which could be the result of a couple of factors:

1. In fluid mechanics, forces can grow exponentially with respect to speed changes.
2. The structure that houses the rotating fan blade could have a vibrational harmonic around these speeds (more on this in the next blog).

## Spectrogram Signal Analysis

The FFT algorithm is helpful for visualizing stationary signals, where frequency energies aren’t changing over time, but when signals aren’t stationary a spectrogram is preferred. The spectrogram batches the signal and performs an FFT on each batch, then plots the FFT’s w.r.t. time using color intensity to indicate amplitude for frequencies of interest.

My desk fan has 4 different speed settings. I took a 45 second clip with the Nicla in the same position. Over the experiment, I increased the speed every ~10 seconds. We can then create the spectrogram of this experiment using the following scipy function.

The spectrogram displays 3 Dimensions of information in a 2D plot, those are

1. Time on the x-axis
2. Frequency on the y-axis
3. Amplitude (or phase if desired) as color intensity

Every 10 seconds, the desk fan is moved to a higher speed (marked by the red lines). After each increase, we can see that the dominant frequency moves to a higher speed and that the amplitude of the dominant frequency is also increasing. Great!

Now that you’re armed with one of the most powerful engineering algorithms in recent history, we’re going to explore a fun real-world application of FFT in the next lesson by making a musical tuner. See you then!