0
0
SciPydata~5 mins

Why Fourier transforms reveal frequencies in SciPy

Choose your learning style9 modes available
Introduction

Fourier transforms help us find the hidden repeating patterns (frequencies) inside any signal or data. This is useful because many real-world signals are made of different waves combined together.

You want to find the main tones in a sound recording.
You need to analyze the heartbeat signals to check for irregularities.
You want to detect repeating patterns in stock market prices.
You want to filter out noise from sensor data by focusing on certain frequencies.
You want to understand the rhythm or cycles in weather data.
Syntax
SciPy
from scipy.fft import fft

# fft(data) computes the Fourier transform of the data array

The input data should be a list or array of numbers representing the signal over time.

The output is a complex array showing amplitude and phase for each frequency component.

Examples
This finds the frequency components of a 5 Hz sine wave sampled 100 times over 1 second.
SciPy
from scipy.fft import fft
import numpy as np

# Simple sine wave
x = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100, endpoint=False))
result = fft(x)
This signal combines 3 Hz and 10 Hz waves. Fourier transform will show peaks at these frequencies.
SciPy
from scipy.fft import fft
import numpy as np

# Signal with two frequencies
x = np.sin(2 * np.pi * 3 * np.linspace(0, 1, 100, endpoint=False)) + 0.5 * np.sin(2 * np.pi * 10 * np.linspace(0, 1, 100, endpoint=False))
result = fft(x)
Sample Program

This program creates a signal with two known frequencies, finds its Fourier transform, and prints the main frequencies with their strengths.

SciPy
from scipy.fft import fft, fftfreq
import numpy as np
import matplotlib.pyplot as plt

# Create a signal with two frequencies: 5 Hz and 20 Hz
sampling_rate = 100  # samples per second
T = 1 / sampling_rate  # sample spacing
x = np.linspace(0.0, 1.0, sampling_rate, endpoint=False)
signal = np.sin(2.0 * np.pi * 5.0 * x) + 0.5 * np.sin(2.0 * np.pi * 20.0 * x)

# Compute Fourier transform
yf = fft(signal)
xf = fftfreq(sampling_rate, T)[:sampling_rate // 2]

# Get magnitude of frequencies
magnitude = 2.0 / sampling_rate * np.abs(yf[0:sampling_rate // 2])

# Print main frequencies and their magnitudes
for freq, mag in zip(xf, magnitude):
    if mag > 0.1:
        print(f"Frequency: {freq:.1f} Hz, Magnitude: {mag:.2f}")
OutputSuccess
Important Notes

The Fourier transform output is complex because it contains both amplitude and phase information.

Only half of the output frequencies are unique for real signals, so we usually look at the first half.

Higher magnitude means stronger presence of that frequency in the signal.

Summary

Fourier transforms break down signals into their frequency parts.

This helps us see which frequencies make up the signal.

It is useful for analyzing sounds, vibrations, and many other real-world data.