Skip to main content

How to perform signal processing using SciPy.

Here is a step-by-step tutorial on how to perform signal processing using SciPy.

Step 1: Importing the required libraries

To begin, you need to import the necessary libraries. In this case, we'll be using scipy.signal for signal processing operations and numpy for array manipulation.

import numpy as np
from scipy import signal

Step 2: Creating a signal

Next, let's create a simple signal to work with. For this tutorial, we'll generate a sinusoidal signal.

# Define the parameters of the signal
frequency = 10 # Signal frequency in Hz
amplitude = 1 # Signal amplitude
sampling_rate = 100 # Number of samples per second
duration = 1 # Signal duration in seconds

# Generate the time axis
t = np.linspace(0, duration, int(sampling_rate * duration), endpoint=False)

# Generate the sinusoidal signal
signal_data = amplitude * np.sin(2 * np.pi * frequency * t)

Step 3: Plotting the signal

Before we start processing the signal, let's visualize it using matplotlib.

import matplotlib.pyplot as plt

plt.plot(t, signal_data)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.title('Signal')
plt.show()

Step 4: Applying a filter

Now, let's apply a low-pass filter to the signal using the scipy.signal library. We'll use a Butterworth filter as an example.

# Define the filter parameters
cutoff_frequency = 5 # Cutoff frequency in Hz
filter_order = 4 # Filter order

# Create the Butterworth filter
b, a = signal.butter(filter_order, cutoff_frequency / (sampling_rate / 2), 'low')

# Apply the filter to the signal
filtered_signal = signal.lfilter(b, a, signal_data)

Step 5: Plotting the filtered signal

Let's plot the filtered signal to visualize the effect of the low-pass filter.

plt.plot(t, signal_data, label='Original Signal')
plt.plot(t, filtered_signal, label='Filtered Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.title('Signal with Low-pass Filter')
plt.legend()
plt.show()

Step 6: Performing Fourier Transform

To analyze the frequency content of the signal, we can perform a Fourier Transform using the scipy.fft module.

# Perform the Fourier Transform
signal_spectrum = np.fft.fft(signal_data)

# Generate the frequency axis
frequencies = np.fft.fftfreq(len(signal_data), 1 / sampling_rate)

# Plot the spectrum
plt.plot(frequencies, np.abs(signal_spectrum))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.title('Signal Spectrum')
plt.show()

Step 7: Performing Spectrogram

Lastly, let's generate a spectrogram of the signal using the scipy.signal module.

# Define the spectrogram parameters
window_size = 256
overlap = int(window_size * 0.75)

# Compute the spectrogram
frequencies, times, spectrogram = signal.spectrogram(signal_data, fs=sampling_rate, window='hann',
nperseg=window_size, noverlap=overlap)

# Plot the spectrogram
plt.pcolormesh(times, frequencies, 10 * np.log10(spectrogram), shading='auto')
plt.colorbar(label='Power Spectral Density (dB/Hz)')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.title('Spectrogram')
plt.show()

That's it! You've successfully performed signal processing using SciPy. Feel free to explore more functions and techniques provided by the SciPy library for your specific signal processing needs.