Calculate auto-correlation of a signal to see how it matches with itself at different shifts.
Signal Processing
import numpy as np
signal = [1, 2, 3, 2, 1]
auto_corr = np.correlate(signal, signal, mode='full')
print(auto_corr)
Sample Program
This program calculates the cross-correlation between two signals. It prints the correlation values and the shift where the signals match best. It also shows plots of the signals and their cross-correlation for easy understanding.
Cross-correlation at lag -2: x[0]*y[2]=1*0.5=0.5; lag -1: x[0]*y[1]+x[1]*y[2]=1*1+2*0.5=1+1=2; lag 0: x[0]*y[0]+x[1]*y[1]+x[2]*y[2]=1*0+2*1+3*0.5=0+2+1.5=3.5; lag 1: x[1]*y[0]+x[2]*y[1]=2*0+3*1=3; lag 2: x[2]*y[0]=3*0=0
Step 2: Arrange results in full mode order
Full mode output is [lag -2, -1, 0, 1, 2] = [0.5, 2, 3.5, 3, 0]
Final Answer:
[0.5, 2, 3.5, 3, 0] -> Option C
Quick Check:
Cross-correlation values match calculated array [OK]
Hint: Calculate lag by lag multiplying overlapping elements [OK]
Common Mistakes:
Mixing convolution with correlation
Incorrect lag order in output
Forgetting zero padding in full mode
4. You run numpy.correlate(signal1, signal2) but get a shorter output than expected. What is the likely cause?
medium
A. The function requires 2D arrays, not 1D
B. signal1 and signal2 must be the same length
C. You need to normalize signals before correlation
D. The default mode is 'valid', which returns only overlapping parts
Solution
Step 1: Check default mode of numpy.correlate
By default, numpy.correlate uses mode='valid', which returns only positions where signals fully overlap, resulting in shorter output.
Step 2: Understand effect of mode on output length
To get full cross-correlation including partial overlaps, mode='full' must be specified.
Final Answer:
The default mode is 'valid', which returns only overlapping parts -> Option D
Quick Check:
Default mode='valid' shortens output [OK]
Hint: Specify mode='full' for complete cross-correlation output [OK]
Common Mistakes:
Assuming signals must be same length
Thinking normalization affects output length
Believing function needs 2D arrays
5. You have two noisy signals recorded with a delay. How can cross-correlation help you find the delay between them?
hard
A. By finding the lag where cross-correlation is maximum, indicating best alignment
B. By subtracting the signals and measuring the difference
C. By computing the Fourier transform and comparing magnitudes
D. By averaging the signals and checking the mean value
Solution
Step 1: Understand cross-correlation use for delay detection
Cross-correlation slides one signal over the other and measures similarity at each lag.
Step 2: Identify lag with maximum cross-correlation
The lag where cross-correlation peaks shows the delay that best aligns the two signals despite noise.
Final Answer:
By finding the lag where cross-correlation is maximum, indicating best alignment -> Option A
Quick Check:
Max cross-correlation lag = signal delay [OK]
Hint: Max cross-correlation lag reveals signal delay [OK]
Common Mistakes:
Using subtraction which ignores alignment
Confusing frequency domain methods here
Averaging signals loses timing info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.