I am trying to implement a code in MATLAB to show harmonic content and order in Matlab. my fundamental is supposed to be at harmonic order 1, however the ouput plot I am getting is wrong and I am having difficulies proceeding. I am a first time user of this forum and i have posted the same question in the matlab site
Here : I would really apreciate any help. Thanks

[D,S,R] = xlsread('test data.xls');    
v = D(:,2);
Signal = D(:,2);                                                 
Ts = 0.00005;                                               % Sampling Interval (seconds)
Fs = 1/Ts;                                                  % Sampling Frequency (Hz)
Fn = Fs/2; 
N = length(Signal);
meanSignal = mean(Signal);                                  % ‘Signal’ Mean
FTSignal = fft(Signal-meanSignal)/N;                    
Fv = linspace(0, 1, fix(numel(FTSignal)/2)+1)*Fn;           % Frequency Vector
Iv = 1:numel(Fv);                                           % Index Vector
%Plotting Harmonics (THD)
har_mag = abs(FTSignal(Iv))*2;
xlabel('Harmonic Order','fontsize',10);
ylabel('Voltage (V)','fontsize',10)
hold off

False reading of the output I am getting is this: enter image description here

the output should look similar to figure below:

enter image description here

  • $\begingroup$ Could you show some plots of what your result looks like? It would make it easier to get responses than having to download the excel file and run your code etc... $\endgroup$ – Dan Boschen May 4 at 1:18
  • $\begingroup$ Hi Dan. Thanks for responding. I have edited my post to include2 plots. The first plot is the ouput i am getting upon running the code. The second is the ouput the code supposed to be showing. Thanks. $\endgroup$ – Jmv May 4 at 2:20
  • 2
    $\begingroup$ You confuse the resolution of an FFT with the fundamental of your signal. If Fs = 1 kHz and the number of points of your FFT is 1000 for example, the resolution of your FFT will be 1 Hz. So assuming your fundamental is 60 Hz, then the 61th bin would correspond to your 60-Hz fundemental. $\endgroup$ – Ben May 4 at 3:07

Yes you are correct that you are indeed seeing the equivalent of what would be the 15th harmonic of the fundamental frequency of the equivalent Fourier Series Expansion of the resulting equivalent continuous time waveform that was truncated in time to 40 samples (2 ms). According to the Fourier Series Expansion, $T$ would be 2 ms and the fundamental frequency for any continuous time domain trajectory over duration $T$ would be $1/T$. That fundamental frequency need not be the largest frequency nor need it be non-zero. The sum of that frequency with it's initial phase and amplitude with all the higher integer harmonics will converge to the original waveform.

That is a rather convoluted but mathematically accurate view. You are also seeing the expected result of the Discrete Fourier Transform for what must be a dominantly sinusoidal waveform that has a frequency of 15/40 of the sample rate (at or near 7.5 KHz). You are not sampling high enough to uniquely see higher order harmonics as they would be folded into the first Nyquist Zone from DC to $f_s/2$. (For example the 2nd harmonic at or near 15KHz would fold to 5KHz given your 20KHz sampling rate, or bin #10 in the plot above, but the signal may also have phase or amplitude variation causing such a sideband that is 2.5 KHz away- once sampled you can't distinguish these two effects). You could certainly rotate (frequency translate) your signal to move to a lower bin as in your desired plot, but that will not resolve the ambiguity of the harmonics unless you do that frequency translation prior to sampling (as well as low pass filter out any signals that may still exist above $f_s/2$.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.