Signal properties (amplitude, frequency, phase) in Signal Processing - Time & Space Complexity
We want to understand how the time to analyze signal properties changes as the signal length grows.
How does the work needed to find amplitude, frequency, and phase grow with input size?
Analyze the time complexity of the following code snippet.
import math
# signal is a list of sampled values
n = len(signal)
amplitude = max(signal) - min(signal)
# frequency estimation using zero crossings
zero_crossings = 0
for i in range(1, n):
if (signal[i-1] < 0 and signal[i] >= 0) or (signal[i-1] >= 0 and signal[i] < 0):
zero_crossings += 1
frequency = zero_crossings / (2 * duration)
# phase estimation (simplified)
phase = math.atan2(signal[1] - signal[0], 1) # example calculation
This code calculates amplitude by finding max and min, estimates frequency by counting zero crossings, and computes a simple phase estimate.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over the signal samples to count zero crossings.
- How many times: The loop runs once for each sample except the first, so about n-1 times.
As the signal length grows, the number of steps to find amplitude and count zero crossings grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (finding max, min, and zero crossings) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The work grows roughly in direct proportion to the signal length.
Time Complexity: O(n)
This means the time to analyze the signal grows linearly as the signal gets longer.
[X] Wrong: "Counting zero crossings takes constant time regardless of signal length."
[OK] Correct: You must check each pair of samples to find zero crossings, so the time grows with the number of samples.
Understanding how signal analysis time grows helps you explain your approach clearly and shows you can think about efficiency in real tasks.
"What if we used a more complex frequency estimation method that compares every sample to every other sample? How would the time complexity change?"
Practice
Solution
Step 1: Understand amplitude meaning
Amplitude measures the height or strength of a wave, showing how strong the signal is.Step 2: Compare with other properties
Frequency is how often the wave repeats, phase shifts the start, wavelength is distance, not height.Final Answer:
Amplitude -> Option BQuick Check:
Amplitude = wave height [OK]
- Confusing amplitude with frequency
- Thinking phase affects height
- Mixing wavelength with amplitude
t = np.linspace(0, 1, 500)
y = ?Solution
Step 1: Recall sine wave formula
The sine wave formula is amplitude * sin(2π * frequency * time).Step 2: Match formula to options
3 * np.sin(2 * np.pi * 5 * t) matches amplitude 3 and frequency 5 correctly as 3 * sin(2π * 5 * t).Final Answer:
3 * np.sin(2 * np.pi * 5 * t) -> Option CQuick Check:
Amplitude * sin(2π * frequency * t) [OK]
- Multiplying frequency by amplitude inside sine
- Adding amplitude instead of multiplying
- Swapping amplitude and frequency
import numpy as np
t = np.linspace(0, 1, 5)
y = 2 * np.sin(2 * np.pi * 1 * t + np.pi/2)
print(np.round(y, 2))What is the output?
Solution
Step 1: Calculate time points
t = [0, 0.25, 0.5, 0.75, 1]Step 2: Compute y values
y = 2 * sin(2π * 1 * t + π/2) = 2 * cos(2π * t) due to phase shift π/2.
Values: 2*cos(0)=2, 2*cos(π/2)=0, 2*cos(π)=-2, 2*cos(3π/2)=0, 2*cos(2π)=2
np.round(y, 2) prints [ 2. 0. -2. 0. 2. ]Final Answer:
[ 2. 0. -2. 0. 2. ] -> Option AQuick Check:
Phase π/2 shifts sine to cosine [OK]
- Ignoring phase shift
- Rounding errors
- Mixing sine and cosine values
import numpy as np
t = np.linspace(0, 1, 100)
y = 4 * np.sin(2 * np.pi * t) * 2Solution
Step 1: Analyze sine argument order
The sine argument is 2π * t (frequency 1 Hz), but *2 is outside sin(), incorrectly scaling the amplitude instead of frequency.Step 2: Correct placement for frequency
Frequency multiplier must be inside sine: 2 * np.pi * 2 * t.Final Answer:
The frequency multiplier is in the wrong place; it should be inside the sine argument as 2 * np.pi * 2 * t -> Option DQuick Check:
Frequency must multiply time inside sine [OK]
- Multiplying amplitude incorrectly
- Using wrong time vector type
- Changing linspace range unnecessarily
y = 5 * np.sin(2 * np.pi * 3 * t + π/4). If you want to double the frequency and halve the amplitude while keeping the phase shift the same, which expression is correct?Solution
Step 1: Adjust amplitude and frequency
Original amplitude is 5, halved is 2.5. Original frequency is 3, doubled is 6.Step 2: Keep phase shift same
Phase shift π/4 remains unchanged in the new expression.Final Answer:
y = 2.5 * np.sin(2 * np.pi * 6 * t + π/4) -> Option AQuick Check:
Amplitude halved, frequency doubled, phase same [OK]
- Changing phase shift incorrectly
- Multiplying amplitude instead of halving
- Swapping frequency and amplitude values
Ready to prove it?
Pass the test and this pattern moves unknown
