I would like to view code that i can type in order to simulate all harmonics of a simple sine wave within matlab. Thank you.

1 view (last 30 days)
I would like to perform harmonic analysis on a sine wave and see their distribution as the multiples increase in magnitude of a 50Hz 400V sine wave. Please help.

Answers (1)

Walter Roberson
Walter Roberson on 6 Jan 2016
Sample code.
fig = figure('Units', 'normal', 'Position', [0 0 .9 .9]);
f0 = 50; %50 Hz
T = 1; %for 1 seconds. Can be less than 1
FS = 500; %sampled at 500 Hz
hL = floor(T/2*FS); %half length
samp_per_Hz = f0*T;
H0 = 1; %power of fundamental
for H1 = linspace(0,5); %progression of first harmonic
f = zeros(1, hL);
f(floor(samp_per_Hz)) = H0;
f(floor(2*samp_per_Hz)) = H1;
w = [0, f, fliplr(f)]; %symmetric with 0 power
y = ifft(w);
t = (0:length(w)-1)/FS;
plot(t, y);
pause(0.1);
end
Your FS (sampling frequency) needs to be at least twice your fundamental. The higher you raise your FS the more points it will need to fit across the axes and so the closer together the peaks will get; but at the same time the higher the FS the smoother the curves (especially when you zoom.)
  3 Comments
Rebecca Bisangwa
Rebecca Bisangwa on 7 Jan 2016
by the way what are the axes values that you used? because I think I would like to plot an amplitude vs frequency graph for this one. So should I just use FFT?
Walter Roberson
Walter Roberson on 7 Jan 2016
The x axes is time in seconds. The y axes is, I guess, power per frequency, but not in decibels (see http://www.mathworks.com/help/signal/ug/psd-estimate-using-fft.html?BB=1)
Amplitude vs frequency would be
plot(w);
in its anti-symmetric form, or you could fftshift(w) to put frequency 0 (DC component) at the center. Labeling the x axes on such plots is always a nuisance.

Sign in to comment.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!