Signal addition and multiplication in Signal Processing - Time & Space Complexity
We want to see how the time needed to add or multiply signals changes as the signal length grows.
How does the number of steps grow when signals get longer?
Analyze the time complexity of the following code snippet.
# Assume signals are lists of numbers
signal1 = [1, 2, 3, 4]
signal2 = [5, 6, 7, 8]
# Add signals element-wise
added_signal = [x + y for x, y in zip(signal1, signal2)]
# Multiply signals element-wise
multiplied_signal = [x * y for x, y in zip(signal1, signal2)]
This code adds and multiplies two signals element by element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the signals to add or multiply.
- How many times: Once for each element in the signals, so as many times as the signal length.
As the signal length grows, the number of additions and multiplications grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions and 10 multiplications |
| 100 | About 100 additions and 100 multiplications |
| 1000 | About 1000 additions and 1000 multiplications |
Pattern observation: The work grows directly with the signal length.
Time Complexity: O(n)
This means the time to add or multiply signals grows in a straight line with the signal length.
[X] Wrong: "Adding two signals takes constant time no matter how long they are."
[OK] Correct: Each element must be processed, so longer signals need more steps.
Understanding how signal operations scale helps you explain efficiency clearly and shows you know how data size affects processing time.
"What if we added a nested loop to multiply every element of one signal by every element of the other? How would the time complexity change?"
Practice
Solution
Step 1: Understand signal addition
Signal addition means adding each value of one signal to the corresponding value of the other signal.Step 2: Apply point-by-point addition
For signals of the same length, each position's values are summed to form the new signal.Final Answer:
You get a new signal where each value is the sum of the corresponding values. -> Option CQuick Check:
Signal addition = sum of values [OK]
- Confusing addition with multiplication
- Thinking signals concatenate instead of adding
- Assuming subtraction instead of addition
a and b element-wise?Solution
Step 1: Understand element-wise multiplication
We multiply each element of listaby the corresponding element of listb.Step 2: Use list comprehension with index
The syntax[a[i] * b[i] for i in range(len(a))]correctly multiplies elements point by point.Final Answer:
result = [a[i] * b[i] for i in range(len(a))] -> Option BQuick Check:
Element-wise multiply = list comprehension [OK]
- Using
a * bwhich repeats list instead of multiply - Using undefined function
multiply - Using addition instead of multiplication
a = [1, 3, 5] and b = [2, 4, 6], what is the result of their element-wise addition?Solution
Step 1: Add corresponding elements of signals
Calculate 1+2=3, 3+4=7, 5+6=11 for each position.Step 2: Form the new signal list
The resulting list is [3, 7, 11].Final Answer:
[3, 7, 11] -> Option AQuick Check:
Sum each pair = [3, 7, 11] [OK]
- Adding all values together instead of point-wise
- Confusing addition with multiplication
- Concatenating lists instead of adding
signal1 = [1, 2, 3] signal2 = [4, 5] result = [signal1[i] * signal2[i] for i in range(len(signal1))]
What is the main problem with this code?
Solution
Step 1: Check signal lengths
signal1has length 3,signal2has length 2, so they differ.Step 2: Identify index error cause
Loop runs 3 times, butsignal2[2]does not exist, causing an error.Final Answer:
The signals have different lengths causing an index error. -> Option AQuick Check:
Different lengths cause index error [OK]
- Ignoring length mismatch
- Blaming operator instead of index range
- Assuming list comprehension syntax is wrong
temp = [20, 22, 21, 23] and humidity = [0.5, 0.6, 0.55, 0.65].You want to create a new signal that represents the heat index by multiplying temperature and humidity point by point.
What is the correct Python code to do this?
Solution
Step 1: Confirm signals have same length
Bothtempandhumidityhave length 4, so element-wise operations are valid.Step 2: Use list comprehension for multiplication
Multiply each temperature by corresponding humidity using[temp[i] * humidity[i] for i in range(len(temp))].Final Answer:
heat_index = [temp[i] * humidity[i] for i in range(len(temp))] -> Option DQuick Check:
Element-wise multiply with list comprehension [OK]
- Using
temp * humiditywhich repeats list - Adding instead of multiplying signals
- Using undefined function
multiply
Ready to prove it?
Pass the test and this pattern moves unknown
