Harmonic distortion of a input signal
Terry Jan Reedy
tjreedy at udel.edu
Sun May 19 19:19:55 EDT 2013
On 5/19/2013 6:49 PM, Oscar Benjamin wrote:
>>>> import numpy as np
>
> Create a square wave signal:
>
>>>> x = np.zeros(50)
>>>> x[:25] = -1
>>>> x[25:] = +1
>>>> x
> array([-1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1.,
> -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., -1., 1.,
> 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
> 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
>
> Compute the magnitude spectrum:
>
>>>> spect = abs(np.fft.fft(x)[:25])
>>>> spect
> array([ 0. , 31.85194222, 0. , 10.67342282,
> 0. , 6.47213595, 0. , 4.69726931,
> 0. , 3.73254943, 0. , 3.13762901,
> 0. , 2.7436023 , 0. , 2.47213595,
> 0. , 2.28230601, 0. , 2.15105461,
> 0. , 2.06487174, 0. , 2.01589594, 0. ])
>
> Find the index of the maximum element:
>
>>>> np.argmax(spect)
> 1
>
> So the peak is the lowest non-zero frequency component of the DFT. In
> Hz this corresponds to a frequency of 1/T where T is the duration of
> the signal.
While you were answering a specific question, I think the above is a
nice tutorial example, because it is more meaningful than arbitrary
operations applied to random data.
More information about the Python-list
mailing list