Data Science Machine Learning Data Analysis
37.9K subscribers
2.79K photos
30 videos
39 files
1.26K links
This channel is for Programmers, Coders, Software Engineers.

1- Data Science
2- Machine Learning
3- Data Visualization
4- Artificial Intelligence
5- Data Analysis
6- Statistics
7- Deep Learning

Cross promotion and ads: @hussein_sheikho
Download Telegram
Topic: Python SciPy – From Easy to Top: Part 6 of 6: Signal Processing, Interpolation, and Fourier Transforms

---

1. Introduction

SciPy contains powerful tools for signal processing, interpolation, and Fourier transforms. These are essential in fields like image and audio processing, scientific simulations, and data smoothing.

Main submodules covered in this part:

scipy.signal – Signal processing
scipy.fft – Fast Fourier Transform
scipy.interpolate – Data interpolation and curve fitting

---

### 2. Signal Processing with `scipy.signal`

Filtering a Signal:

Let’s create a noisy sine wave and apply a low-pass filter.

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Create a sample signal with noise
t = np.linspace(0, 1.0, 200)
x = np.sin(2 * np.pi * 5 * t) + 0.5 * np.random.randn(200)

# Apply a Butterworth low-pass filter
b, a = signal.butter(3, 0.2)
filtered = signal.filtfilt(b, a, x)

# Plot original and filtered signals
plt.plot(t, x, label="Noisy Signal")
plt.plot(t, filtered, label="Filtered Signal")
plt.legend()
plt.title("Low-pass Filtering with Butterworth")
plt.show()


---

Find Peaks in a Signal:

peaks, _ = signal.find_peaks(x, height=0)
print("Peak Indices:", peaks)


---

### 3. Fourier Transform with `scipy.fft`

The Fourier Transform breaks a signal into its frequency components.

from scipy.fft import fft, fftfreq

# Number of sample points
N = 600
# Sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N, endpoint=False)
y = np.sin(50.0 * 2.0 * np.pi * x) + 0.5 * np.sin(80.0 * 2.0 * np.pi * x)

yf = fft(y)
xf = fftfreq(N, T)[:N//2]

plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.title("Fourier Transform of Signal")
plt.show()


---

### 4. Interpolation with `scipy.interpolate`

Interpolation estimates unknown values between known data points.

from scipy import interpolate

x = np.linspace(0, 10, 10)
y = np.sin(x)

# Create interpolating function
f = interpolate.interp1d(x, y, kind='cubic')

# Interpolate new values
xnew = np.linspace(0, 10, 100)
ynew = f(xnew)

plt.plot(x, y, 'o', label="Data Points")
plt.plot(xnew, ynew, '-', label="Cubic Interpolation")
plt.legend()
plt.title("Interpolation Example")
plt.show()


---

### 5. 2D Interpolation Example

from scipy.interpolate import griddata

# Known points
points = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
values = np.array([0, 1, 1, 0])

# Interpolation grid
grid_x, grid_y = np.mgrid[0:1:100j, 0:1:100j]
grid_z = griddata(points, values, (grid_x, grid_y), method='cubic')

plt.imshow(grid_z.T, extent=(0,1,0,1), origin='lower')
plt.title("2D Cubic Interpolation")
plt.colorbar()
plt.show()


---

### 6. Summary

scipy.signal is used for filtering, finding peaks, convolution, etc.
scipy.fft helps analyze signal frequencies.
scipy.interpolate estimates unknown values smoothly between data points.

These tools are critical for real-time data analysis, image/audio processing, and engineering applications.

---

Exercise

• Generate a noisy signal and apply both low-pass and high-pass filters.
• Plot the Fourier transform of a composed signal of multiple frequencies.
• Perform cubic interpolation on a dataset with missing values and plot both.

---

#Python #SciPy #SignalProcessing #FFT #Interpolation #ScientificComputing

https://t.iss.one/DataScienceM
7