Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Filtering Signals with Python | Signal Processing for Electrical Engineers
Python for Electrical Engineers

bookFiltering Signals with Python

Digital filtering is a fundamental technique in signal processing that allows you to modify or enhance electrical signals by removing unwanted components such as noise. In electrical engineering, signals captured from sensors or communication systems often contain random fluctuations or interference that can obscure the true information. Digital filters are algorithms that process these signals to suppress noise and improve clarity, making them essential for reliable measurements, control systems, and data analysis.

12345678910
import numpy as np def moving_average(signal, window_size): return np.convolve(signal, np.ones(window_size)/window_size, mode='valid') # Example usage: np.random.seed(0) signal = np.random.normal(0, 1, 100) # Simulated noisy signal filtered_signal = moving_average(signal, window_size=5) print(filtered_signal[:10]) # Show first 10 filtered values
copy

A moving average filter works by replacing each point in a signal with the average of its neighboring values over a fixed window. This approach smooths out rapid fluctuations caused by noise while preserving the general trend of the original signal. By averaging adjacent points, the filter reduces the impact of outlier values, resulting in a cleaner, more stable signal. However, it can also slightly blur sharp transitions or edges in the data, so the choice of window size is important: a larger window provides more smoothing but may reduce signal detail.

12345678910111213141516171819202122232425
import matplotlib.pyplot as plt # Generate a noisy sine wave t = np.linspace(0, 2*np.pi, 200) clean_signal = np.sin(t) noise = np.random.normal(0, 0.3, t.shape) noisy_signal = clean_signal + noise # Apply moving average filter window_size = 10 filtered_signal = moving_average(noisy_signal, window_size) # Adjust time axis for filtered signal t_filtered = t[(window_size-1):] # Plot results plt.figure(figsize=(10, 5)) plt.plot(t, noisy_signal, label='Noisy Signal', alpha=0.6) plt.plot(t, clean_signal, label='Original Signal', linestyle='--') plt.plot(t_filtered, filtered_signal, label='Filtered Signal', linewidth=2) plt.legend() plt.xlabel('Time') plt.ylabel('Amplitude') plt.title('Moving Average Filter Applied to Noisy Sine Wave') plt.show()
copy

1. What is the purpose of a moving average filter?

2. How does filtering improve signal quality?

3. What are some real-world applications of digital filters?

question mark

What is the purpose of a moving average filter?

Select the correct answer

question mark

How does filtering improve signal quality?

Select the correct answer

question mark

What are some real-world applications of digital filters?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how the moving average filter works in more detail?

What are some other types of digital filters besides moving average?

How do I choose the best window size for my signal?

bookFiltering Signals with Python

Scorri per mostrare il menu

Digital filtering is a fundamental technique in signal processing that allows you to modify or enhance electrical signals by removing unwanted components such as noise. In electrical engineering, signals captured from sensors or communication systems often contain random fluctuations or interference that can obscure the true information. Digital filters are algorithms that process these signals to suppress noise and improve clarity, making them essential for reliable measurements, control systems, and data analysis.

12345678910
import numpy as np def moving_average(signal, window_size): return np.convolve(signal, np.ones(window_size)/window_size, mode='valid') # Example usage: np.random.seed(0) signal = np.random.normal(0, 1, 100) # Simulated noisy signal filtered_signal = moving_average(signal, window_size=5) print(filtered_signal[:10]) # Show first 10 filtered values
copy

A moving average filter works by replacing each point in a signal with the average of its neighboring values over a fixed window. This approach smooths out rapid fluctuations caused by noise while preserving the general trend of the original signal. By averaging adjacent points, the filter reduces the impact of outlier values, resulting in a cleaner, more stable signal. However, it can also slightly blur sharp transitions or edges in the data, so the choice of window size is important: a larger window provides more smoothing but may reduce signal detail.

12345678910111213141516171819202122232425
import matplotlib.pyplot as plt # Generate a noisy sine wave t = np.linspace(0, 2*np.pi, 200) clean_signal = np.sin(t) noise = np.random.normal(0, 0.3, t.shape) noisy_signal = clean_signal + noise # Apply moving average filter window_size = 10 filtered_signal = moving_average(noisy_signal, window_size) # Adjust time axis for filtered signal t_filtered = t[(window_size-1):] # Plot results plt.figure(figsize=(10, 5)) plt.plot(t, noisy_signal, label='Noisy Signal', alpha=0.6) plt.plot(t, clean_signal, label='Original Signal', linestyle='--') plt.plot(t_filtered, filtered_signal, label='Filtered Signal', linewidth=2) plt.legend() plt.xlabel('Time') plt.ylabel('Amplitude') plt.title('Moving Average Filter Applied to Noisy Sine Wave') plt.show()
copy

1. What is the purpose of a moving average filter?

2. How does filtering improve signal quality?

3. What are some real-world applications of digital filters?

question mark

What is the purpose of a moving average filter?

Select the correct answer

question mark

How does filtering improve signal quality?

Select the correct answer

question mark

What are some real-world applications of digital filters?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
some-alt