Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

THD Calculation of a Sine wave in Matlab

Status
Not open for further replies.

smalandr

Newbie level 4
Joined
Oct 29, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,328
Hi,
can anyone help me to calculate the THD in a frequency response of a sine wave?

I know the theory behind it, but cannot find a way to write the Matlab code.

Many thanks,
Stef
 

Hi there,
If you have your output signal as a vector and you already know the fundamental, I would suggest zero padding the vector (adding zeros to the beginning of the vector and the end, in order to increase your FFT resolution) and taking the FFT. Zero padding may not be necessary.

Mathworks site that describes the FFT function is really helpful for gettings your hands dirty : Discrete Fourier transform - MATLAB. This should show you how to pick out the fundamental and then pick out your harmonics. Of course, this super simple approach will only work if your highest harmonic under consideration is below the Nyquist rate.
T
 

Hello, thanks for your answer.
However I have enough resolution in my fft for now.
The problem is that I want to find the highest harmonic not visually but with a script. Then I would calculate the THD ...
I have seen some functions do that but I want to do it from scratch.
Any ideas?
Thanks ...
Stef
 

I figured as much. But if you know the fundamental then you're all set. Are you saying you don't assume anything about the input?

If so, why not implement a peak search in the frequency spectrum and assume the lowest frequency peak (non-DC) is the fundamental.
 

I figured as much. But if you know the fundamental then you're all set. Are you saying you don't assume anything about the input?

If so, why not implement a peak search in the frequency spectrum and assume the lowest frequency peak (non-DC) is the fundamental.

Hey,
this is how I'm trying to do this :

[a,b]=max(Sig24) % In order to find where is the peak/fundamental.


i=find(fr<20000); % Find the audible frequency region
P=Sig24_nodB(i); % Find the power of each frequency component in audible region // Sig24_nodB is the abs(fft()) of my signal.

P=P.^2; % to find the power ... ^ 2 ...

num = sum(P)- P(b); % The power of all - the power of the fundamental
den = P(b);

THD_N=(num/den)


Any ideas why it doesn't give me the right answer?
Thanks.
Stef
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top