Harmonic spectrum of the voltage data
11 views (last 30 days)
Show older comments
danyang yao
on 18 Jul 2019
Commented: Star Strider
on 11 Jan 2022
Hi all,
I am new to matlab and have this small questions. So basically I need to plot the harmonic spectrum for the data that I have recorded in a txt file. I managed to import the data into matlab but don't know how to plot the cooresponding hramonic spectrum. The data is in the attachement. Thanks in advances:)
0 Comments
Accepted Answer
Star Strider
on 18 Jul 2019
Try this:
D = dlmread('voltage_u.txt','\t', 4, 0);
D = D(1:end-1,1:2); % First Two Columns (Third is 0), Elkiminat (0.0) In Last Row
t = D(:,1); % Time
V = D(:,2); % Volts
L = size(D,1); % Signal Length
tv = linspace(min(t), max(t), L); % Create Regularly-Spaced Time Vector
Ts = tv(2)-tv(1); % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
Vv = resample(V, t, Fs); % Resample To Constant Sampling Rate
Fn = Fs/2; % Nyquist Frequency
FVv = fft(Vv)/L; % Discrete Fourier Transform
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
semilogy(Fv, abs(FVv(Iv))*2)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude (V)')
Your time-domain data are not regularly-sampled, so it is necessary to use the resample function to provide a uniformly-sampled vector in order for the fft function to produce the correct results.
Explore this at your leisure to understand how it works.
2 Comments
Zong-Jhen Ye
on 10 Jan 2022
Edited: Zong-Jhen Ye
on 11 Jan 2022
Hi Strider,
Thank you so much for the sharing. There is one thing I think we can modeify, which is "Fn = Fs/2; % Nyquist Frequency" in your code. I think it could be "Fn = Fs/3;". The reason is if the data is voltage, the fundamental frequency should be 60 Hz. If we use "Fn = Fs/2", the first peak is at 90 Hz, which do not make sense. But the first peak (i.e. fundamental frequency) will be at 60 Hz if we use "Fn = Fs/3;".
Please let me know if I misunderstanding anything.
Star Strider
on 11 Jan 2022
You are misunderstanding the Nyquist frequency. It is the highest frequency that can uniquely be identified in a sampled signal, and is defined as ‘Fn=Fs/2’ and my defining it that way is not an error. Modern ADC uints all have analog lowpass Bessel fiilters at the input that reject all frequencies greater than the Nyquist frequency of the ADC (the reason that only a llimited number of sampling frequencies are available) in order to prevent aliasing.
.
More Answers (0)
See Also
Categories
Find more on Spectral Measurements 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!