Reputation: 249
Is there a simpler Way to construct frequency components after FFT?
I am computing the FFT of a signal and normalizing it as follows:
import numpy as np
from scipy.fftpack import fft
# By calculating FFT...
Y = fft(y) / n # FFT computing and normalization
# Constructing a new list with one-sided frequency range
Y = [Y[0]] + [2 * Y[i] for i in range(1, n//2)]
This works correctly, but I initially attempted a more compact form:
Y = [Y[0]] + [2 * Y[range(1, n//2)]] # This does NOT work
which will actually increase to 2D instead of concatenating indices from 0 to 1:n//2.
My question is: Is there a simpler or more Pythonic way to achieve this one-sided frequency range construction? Ideally, something cleaner than my working solution while maintaining efficiency.
Upvotes: -1
Views: 66
Reputation: 249
Finally, I found that the following unpack operator can work for this purpose.
Y= [Y[0],*Y[1:n//2]*2]
Since we can have
>>>a, *b, c = [1, 2, 3, 4, 5] to
>>>b
>>>[2, 3, 4]
Also if
>>>a = [*'PYTHON']
>>>a
>>>['P', 'Y', 'T', 'H', 'O', 'N']
Upvotes: -1