Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Harmonic distortion of a input signal

68 views
Skip to first unread message

Anti Log

unread,
May 19, 2013, 6:52:27 AM5/19/13
to
Hi,
I have a task to calculate total distortion of a harmonics, of a signal that i imported from oscilloscope as numpy array. I had no problem drawing its spectrum, and time domain graph, but cant seem to find any functions that calculate TDH.
Any help?
Best regards

Chris Angelico

unread,
May 19, 2013, 11:34:53 AM5/19/13
to pytho...@python.org
On Sun, May 19, 2013 at 8:52 PM, Anti Log <antilog...@gmail.com> wrote:
> total distortion of a harmonics

I selected this part of your post, right-clicked, and Chrome offered
to do a Google search for those words. And, surprise surprise, the
first hit is a page that appears to have the mathematics behind it.
Now, I don't know how much you trust Google and Wikipedia, but I'm
sure you can confirm the maths in some other way.

My guess is that there's no function in numpy to do what you're
asking... but it shouldn't be too hard to render the formula/e given
into Python code. Python's pretty expressive when it comes to algebra.
:)

ChrisA

killyb...@gmail.com

unread,
May 19, 2013, 6:25:08 PM5/19/13
to
How can i at least find a peek in FFT spectrum of a square wave ?
From there i could easily build formula. Sorry for bothering but i am new to Python.

Oscar Benjamin

unread,
May 19, 2013, 6:49:52 PM5/19/13
to killyb...@gmail.com, Python List
On 19 May 2013 23:25, <killyb...@gmail.com> wrote:
> How can i at least find a peek in FFT spectrum of a square wave ?
> From there i could easily build formula. Sorry for bothering but i am new to Python.

Are you the same person who posted the original question?

You probably want to use numpy for this. I'm not sure if I understand
your question but here goes:

First import numpy (you may need to install this first):

>>> 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.


Oscar

killyb...@gmail.com

unread,
May 19, 2013, 6:59:17 PM5/19/13
to
Yes, sorry logged from another account.
Would that work on a numpy array ? Because this signal was imported from oscilloscope as a numpy array.
Best regards,

killyb...@gmail.com

unread,
May 19, 2013, 7:03:43 PM5/19/13
to
Got it working, thanks alot :)

Terry Jan Reedy

unread,
May 19, 2013, 7:19:55 PM5/19/13
to pytho...@python.org
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.



killyb...@gmail.com

unread,
May 19, 2013, 7:36:43 PM5/19/13
to
One more question. Function np.argmax returns max of non-complex numbers ?
Because FFT array of my signal is complex.

Gregory Ewing

unread,
May 19, 2013, 9:09:36 PM5/19/13
to
killyb...@gmail.com wrote:
> One more question. Function np.argmax returns max of non-complex numbers ?
> Because FFT array of my signal is complex.

You'll want the magnitudes of the complex numbers.
Actually the squares of the magnitudes (assuming the
data from the oscilloscope represents voltages),
because you're after a ratio of powers.

--
Greg

Dave Angel

unread,
May 19, 2013, 9:11:57 PM5/19/13
to pytho...@python.org
On 05/19/2013 07:36 PM, killyb...@gmail.com wrote:
> One more question. Function np.argmax returns max of non-complex numbers ?
> Because FFT array of my signal is complex.
>

It'd be easier to track the thread if you actually replied to the
message you're responding to, and also if you included some context.
But I'll paste the latter in here:

Terry Reedy said:
> 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


Notice that argmax's argument is the result of an abs() call. It's got
real numbers representing the magnitude of the various complex numbers.

--
DaveA

jmfauth

unread,
May 20, 2013, 1:23:33 PM5/20/13
to
Non sense.

The discrete fft algorithm is valid only if the number of data
points you transform does correspond to a power of 2 (2**n).

Keywords to the problem: apodization, zero filling, convolution
product, ...

eg. http://en.wikipedia.org/wiki/Convolution

jmf

Christian Gollwitzer

unread,
May 20, 2013, 1:50:34 PM5/20/13
to
Am 20.05.13 19:23, schrieb jmfauth:
> Non sense.

Dito.

> The discrete fft algorithm is valid only if the number of data
> points you transform does correspond to a power of 2 (2**n).

Where did you get this? The DFT is defined for any integer point number
the same way.

Just if you want to get it fast, you need to worry about the length. For
powers of two, there is the classic Cooley-Tukey. But there do exist FFT
algorithms for any other length. For example, there is the Winograd
transform for a set of small numbers, there is "mixed-radix" to reduce
any length which can be factored, and there is finally Bluestein which
works for any size, even for a prime. All of the aforementioned
algorithms are O(log n) and are implemented in typical FFT packages. All
of them should result (up to rounding differences) in the same thing as
the naive DFT sum. Therefore, today

> Keywords to the problem: apodization, zero filling, convolution
> product, ...

Not for a periodic signal of integer length.

> eg. http://en.wikipedia.org/wiki/Convolution

How long do you read this group?

Christian

Christian Gollwitzer

unread,
May 20, 2013, 1:56:05 PM5/20/13
to
Oops, I thought we were posting to comp.dsp. Nevertheless, I think
numpy.fft does mixed-radix (can't check it now)

Am 20.05.13 19:50, schrieb Christian Gollwitzer:

Oscar Benjamin

unread,
May 21, 2013, 10:58:15 AM5/21/13
to pytho...@python.org
On 20 May 2013 18:23, jmfauth <wxjm...@gmail.com> wrote:
> Non sense.
>
> The discrete fft algorithm is valid only if the number of data
> points you transform does correspond to a power of 2 (2**n).

As with many of your comments about Python's unicode implementation
you are confusing performance with validity. The DFT is defined and is
a valid invertible map (barring roundoff) for complex vectors of any
integer length. It is also a valid method for understanding the
frequency content of periodic signals. The fastest FFT algorithms are
for vectors whose length is a power of 2 but the other algorithms
produce equally *valid* DFT results.

In the example I posted the computation of the DFT using numpy.fft.fft
was (as far as I could tell) instantaneous. I could use timeit to
discover exactly how many microseconds it took but why when I already
have the results I wanted?

> Keywords to the problem: apodization, zero filling, convolution
> product, ...
>
> eg. http://en.wikipedia.org/wiki/Convolution

These points are not relevant to the example given.


Oscar

jmfauth

unread,
May 23, 2013, 7:44:55 AM5/23/13
to
> >> eg.http://en.wikipedia.org/wiki/Convolution
>
> > How long do you read this group?
>
> >      Christian

------

Forget what I wrote.
I'm understanding what I wanted to say, it is badly
formulated.

jmf

0 new messages