I have a pulse signal that provides a 1 for values of n from 0 to 5. I would like to shift my signal by 20 samples, however all I am doing is increasing my pulse width. What am I doing wrong exactly?
import numpy as np
import matplotlib.pyplot as plt
def pulse_s(t):
return 1 * (t <= 5)
def shift(t,a):
return pulse_s(t-a)
t1 = np.linspace(0,200)
plt.stem(t1,shift(t1,20))
plt.xlabel('n')
plt.ylabel('Amplitude')
plt.title('Pulse Signal p[n] with L = 5 and $n_0$ = 20')
plt.grid(True)
CodePudding user response:
pulse_s does not return a signal that is 1 for all values of n from 0 to 5, but for all values of n smaller than or equal to 5.
This is only equivalent for the input sequence 0, 1, 2, ...
If you "shift" the input sequence by subtracting 20, it becomes the sequence −20, −19, −18, ..., and there are 26 values in this sequence that are smaller than or equal to 5.
To do this properly, you have to explicitly include the lower bound in the pulse_s function (keeping in mind that comparison chaining doesn't work in NumPy as usual):
def pulse_s(t):
return 1 * ((0 <= t) & (t <= 5))
CodePudding user response:
You need to define both ends.
def pulse_s(t):
return 1 * (0 <= t <= 5)
