📖 Scenario: Imagine you have a noisy recording of a bird singing in a park. You want to find out the main melody of the bird's song, but the noise makes it hard to hear. Signal processing helps us clean the noise and find the important parts of the sound.
🎯 Goal: You will learn how to use signal processing to extract useful information from a noisy signal. We will create a simple signal, add noise, filter the noise out, and see the cleaned signal.
📋 What You'll Learn
Create a noisy signal using numpy
Set a threshold frequency for filtering
Use a filter from scipy.signal to remove noise
Print the filtered signal values
💡 Why This Matters
🌍 Real World
Signal processing is used in many areas like cleaning audio recordings, improving medical signals like ECG, and analyzing sensor data.
💼 Career
Understanding how to extract useful information from noisy data is a key skill for data scientists, engineers, and researchers working with real-world data.
Progress0 / 4 steps
1
Create a noisy signal
Create a numpy array called time with 500 points from 0 to 1 using np.linspace(0, 1, 500). Then create a signal called signal as np.sin(2 * np.pi * 5 * time). Finally, create a noisy signal called noisy_signal by adding random noise using np.random.normal(0, 0.5, 500) to signal.
SciPy
Hint
Use np.linspace to create the time points. Use np.sin for the signal. Add noise with np.random.normal.
2
Set the cutoff frequency for filtering
Create a variable called cutoff_freq and set it to 10. This will be the frequency threshold for filtering noise.
SciPy
Hint
Just create a variable named cutoff_freq and assign it the value 10.
3
Filter the noisy signal
Import butter and filtfilt from scipy.signal. Create a Butterworth low-pass filter using butter(4, cutoff_freq, fs=500, btype='low', analog=False) and store the result in b, a. Then apply the filter to noisy_signal using filtfilt(b, a, noisy_signal) and store the result in filtered_signal.
SciPy
Hint
Use butter to create filter coefficients and filtfilt to apply the filter.
4
Print the filtered signal
Print the filtered_signal array to see the cleaned signal values.
SciPy
Hint
Use print(filtered_signal) to show the cleaned signal values.
Practice
(1/5)
1. Why do we use signal processing to extract information from data?
easy
A. To make the data more complex and harder to understand
B. To clean and highlight important parts of the data
C. To delete all the data and keep nothing
D. To randomly change the data values
Solution
Step 1: Understand the purpose of signal processing
Signal processing is used to clean data and focus on useful parts.
Step 2: Identify the correct purpose from options
Only To clean and highlight important parts of the data says it cleans and highlights important data, which matches the purpose.
Final Answer:
To clean and highlight important parts of the data -> Option B
Quick Check:
Signal processing cleans data = A [OK]
Hint: Signal processing cleans data to find useful info [OK]
Common Mistakes:
Thinking signal processing makes data complex
Believing it deletes all data
Assuming it changes data randomly
2. Which of the following is the correct way to import the signal module from scipy?
easy
A. import scipy as signal
B. import signal from scipy
C. import scipy.signal
D. from scipy import signal
Solution
Step 1: Recall Python import syntax
The correct syntax to import a submodule is 'from package import module'.
Step 2: Match options with correct syntax
'from scipy import signal' is correct as it imports the signal module directly into the namespace. 'import scipy as signal' only aliases the scipy package. 'import signal from scipy' is invalid syntax. 'import scipy.signal' requires using scipy.signal.function().
Final Answer:
from scipy import signal -> Option D
Quick Check:
Correct import syntax = C [OK]
Hint: Use 'from scipy import signal' to import correctly [OK]
Common Mistakes:
Using invalid import syntax like 'import signal from scipy'
Forgetting 'from' keyword
Trying to import submodule directly without package
3. What will be the output of this code snippet?
from scipy import signal
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = signal.savgol_filter(x, window_length=3, polyorder=1)
print(np.round(y, 2))
medium
A. [1.33 2. 3. 4. 4.67]
B. [1. 2. 3. 4. 5. ]
C. [1.5 2.5 3.5 4.5 5.5]
D. [0.5 1.5 2.5 3.5 4.5]
Solution
Step 1: Understand what savgol_filter does
The Savitzky-Golay filter smooths data by fitting polynomials in windows.
Step 2: Apply filter to input array
With window_length=3 and polyorder=1, the filter smooths edges slightly, producing values close to but not identical to original.
4. Identify the error in this code that tries to apply a low-pass filter using scipy.signal:
from scipy import signal
import numpy as np
fs = 1000
cutoff = 100
b, a = signal.butter(4, cutoff, btype='low')
data = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs))
filtered = signal.lfilter(b, a, data)
print(filtered[:5])
medium
A. The sampling frequency fs is missing
B. The filter order 4 is too high for this data
C. The cutoff frequency should be normalized between 0 and 1
D. The signal.lfilter function is used incorrectly
Solution
Step 1: Check butter function parameters
scipy.signal.butter expects cutoff (Wn) normalized between 0 and 1 (fraction of Nyquist) when fs is not provided.
Step 2: Examine the butter call
signal.butter(4, cutoff, btype='low') uses cutoff=100 (>1) without fs, causing ValueError.
Step 3: Confirm other options
fs is defined but not passed; order 4 is fine; lfilter usage correct.
Final Answer:
The cutoff frequency should be normalized between 0 and 1 -> Option C
Quick Check:
Without fs, cutoff must be normalized 0-1 = D [OK]
Hint: Check if cutoff frequency needs normalization with butter [OK]
Common Mistakes:
Forgetting to normalize cutoff frequency when fs is missing
Confusing filter order with data size
Misusing lfilter parameters
5. You have noisy heart rate data sampled at 500 Hz. Which scipy.signal method helps extract the true heart rate by removing high-frequency noise?
hard
A. signal.butter with a low-pass filter
B. signal.medfilt to apply a median filter
C. signal.correlate to find repeating patterns
D. signal.spectrogram to visualize frequencies
Solution
Step 1: Understand the problem context
Noisy heart rate data needs noise removal to find true rate, especially removing high-frequency noise.
Step 2: Identify suitable signal processing method
Low-pass filters remove high-frequency noise, preserving slower heart rate signals. Butterworth filter is a common low-pass filter.
Step 3: Evaluate other options
Median filter removes spikes but less effective for frequency noise. Correlate finds patterns but does not filter noise. Spectrogram visualizes frequencies but does not filter.
Final Answer:
signal.butter with a low-pass filter -> Option A
Quick Check:
Low-pass filter removes high-frequency noise = B [OK]
Hint: Use low-pass butter filter to remove high-frequency noise [OK]
Common Mistakes:
Choosing median filter for frequency noise
Using correlate to filter noise
Confusing visualization with filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.