Time scaling (compression and expansion) in Signal Processing - Time & Space Complexity
We want to understand how the time it takes to process a signal changes when we speed it up or slow it down.
How does the work grow when we change the length of the signal by compressing or expanding it?
Analyze the time complexity of the following code snippet.
def time_scale(signal, scale_factor):
scaled_signal = []
for i in range(int(len(signal) / scale_factor)):
index = int(i * scale_factor)
scaled_signal.append(signal[index])
return scaled_signal
This code changes the speed of a signal by picking samples at intervals based on the scale factor.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the scaled signal length and accessing elements from the original signal.
- How many times: Approximately the length of the original signal divided by the scale factor.
As the input signal length grows, the number of operations grows roughly in proportion to the scaled length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 / scale_factor |
| 100 | 100 / scale_factor |
| 1000 | 1000 / scale_factor |
Pattern observation: The work grows linearly with the input size but is reduced by the scale factor.
Time Complexity: O(n / s)
This means the time to process the signal grows linearly with the input length, adjusted by how much we compress or expand it.
[X] Wrong: "Time scaling always takes the same time regardless of the scale factor."
[OK] Correct: Because the loop runs fewer or more times depending on how much the signal is compressed or expanded, the time changes with the scale factor.
Understanding how time changes with signal length and scaling helps you explain performance in real signal processing tasks clearly and confidently.
"What if we used interpolation to fill in values instead of just picking samples? How would the time complexity change?"
Practice
a > 1?Solution
Step 1: Understand time scaling factor meaning
A factora > 1means the time axis is divided by a number greater than 1, making the signal shorter in time.Step 2: Effect on signal playback speed
Shorter time means the signal plays faster, which is called compression.Final Answer:
The signal is compressed and plays faster. -> Option CQuick Check:
Compression (a > 1) = Faster playback [OK]
- Confusing compression with expansion
- Thinking amplitude changes with time scaling
- Mixing frequency effects with time scaling
a to a signal array x using interpolation?Solution
Step 1: Understand time scaling formula
Time scaling by factorameans new time points aret_new = t / a.Step 2: Use interpolation for non-integer indices
Sincet_newmay be fractional, interpolation functionfis used to getx_new.Final Answer:
Code snippet A correctly applies time scaling with interpolation. -> Option DQuick Check:
t_new = t / a with interp1d = Correct scaling [OK]
- Multiplying time by a instead of dividing
- Indexing array directly with fractional indices
- Forgetting to import interp1d
x_scaled?
import numpy as np from scipy.interpolate import interp1d x = np.array([1, 2, 3, 4, 5]) a = 0.5 n = len(x) t = np.arange(n) f = interp1d(t, x, kind='linear', fill_value='extrapolate') t_new = t / a x_scaled = f(t_new)
Solution
Step 1: Check length of t_new
Variablet_newis computed ast / a, butthas length 5, sot_newalso has length 5.Step 2: Interpolation output length
Interpolating at 5 points returns an arrayx_scaledof length 5.Final Answer:
5 -> Option AQuick Check:
Length of t_new = length of x_scaled = 5 [OK]
- Assuming output length changes with scaling factor
- Confusing scaling factor with output size
- Ignoring that t_new length equals original length
import numpy as np from scipy.interpolate import interp1d x = np.array([10, 20, 30, 40]) a = 2 n = len(x) t = np.arange(n) f = interp1d(t, x) t_new = t * a x_expanded = f(t_new)
Solution
Step 1: Recall time scaling formula
To expand (slow down) a signal by factor 2, new time points should bet_new = t / a, nott * a.Step 2: Effect of incorrect scaling
Usingt * acompresses the signal instead of expanding it, causing wrong output.Final Answer:
Using t * a instead of t / a for time scaling. -> Option BQuick Check:
Expansion requires t_new = t / a [OK]
- Multiplying time by a instead of dividing
- Ignoring interpolation domain limits
- Not handling extrapolation in interp1d
x_expanded with proper interpolation and new sampling points?Solution
Step 1: Understand expansion by factor 2
Playing twice as slow means the signal duration doubles, so new time axis goes from 0 tot[-1]*2.Step 2: Create new time points at original sampling rate
New time points should be spaced by1/fsto keep sampling rate consistent.Step 3: Use interpolation with extrapolation
Interpolation function with cubic method andfill_value='extrapolate'handles points beyond original range.Final Answer:
Code snippet A correctly computes the expanded signal. -> Option AQuick Check:
Expansion doubles duration with proper interpolation [OK]
- Reducing time axis length instead of expanding
- Changing sampling interval incorrectly
- Not handling extrapolation in interpolation
Ready to prove it?
Pass the test and this pattern moves unknown
