I'm trying to perform upsampling in Python. Given the signal 's' and factor of upsampling N, I wrote the following code:

> y = np.zeros(N*len(s))
> for i in range(0, N*len(s), N):
>     for j in range(len(s)):
>             y[i] = s[j]
> return y

However, when I have a signal with first value '1' and factor of upsamling 3, my output of the function is like [1, 0, 0, 1, 0, 0] etc, no matter what the rest of the signal looks like. Is there a way to make this code work properly?


I think you are trying to do this:

y = np.zeros(N*len(s))
for i in range(0, len(s)):
    y[i*N] = s[i]

return y

There are other ways in python that avoid explicit loops, for example:

y = [0] * (N * len(s))
y[::N] = s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.