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)
Show older comments
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.
0 Comments
Answers (1)
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
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.
See Also
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!