Harmonic distortion of a input signal

A

Anti Log

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
 
C

Chris Angelico

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
 
K

killybeard91

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

Oscar Benjamin

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):

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:
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
 
K

killybeard91

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,
 
T

Terry Jan Reedy

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

killybeard91

One more question. Function np.argmax returns max of non-complex numbers ?
Because FFT array of my signal is complex.
 
G

Gregory Ewing

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

Dave Angel

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

Oscar Benjamin

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
 
J

jmfauth

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:

------

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

jmf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top