MATLAB Answers

## Harmonic spectrum of the voltage data

Asked by danyang yao

### danyang yao (view profile)

on 18 Jul 2019
Latest activity Answered by Star Strider

### Star Strider (view profile)

on 18 Jul 2019
Accepted Answer by Star Strider

### Star Strider (view profile)

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

Sign in to comment.

## 1 Answer

### Star Strider (view profile)

Answer by Star Strider

### Star Strider (view profile)

on 18 Jul 2019
Accepted Answer

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.

#### 0 Comments

Sign in to comment.