How to perform spectral analysis using fft() function with pre-defined frequency resolution?

1 view (last 30 days)
Hello,
I want to analyse the spectrum of a phase current generated by PWM modulation. The signal itself is stationary and doesn't change in frequency or amplitude over time.
To calculate the spectrum I use the fft() function. Y = fft(X,n) returns the n-point DFT. If no value is specified, Y is the same size as X. That means that the frequency resolution of my resulting spectrum depends on the number of values used for the DFT. I know it is not necessary to define the frequency resolution to analyse the spectrum, but I thought it would be a nice idea. To point out my approach I created a short example shown below.
%measured data
fs = 40960; %Sampling frequency
f = [400, 3000, 5000]; %Frequencies of my function
A = [200, 40 , 20]; %Amplitudes of my function
t = 0:1/fs:2; %time vector
%Function
U = A(1)*sin(2*pi*f(1)*t) + A(2)*sin(2*pi*f(2)*t) + A(3)*sin(2*pi*f(3)*t);
%Calculation Parameter
fres = 25; %desired frequency resolution
N = length(U); %length of the signal
nfft = 2^nextpow2((fs/fres)-1); %number of spectral lines --> power of 2 because radix-2-algorithm
fRes = fs/(nfft+1); %real frequency resolution depending on nfft
ns = floor(N/nfft); %number of sections
%Frequency axis
fax = linspace(0,fs,nfft+1);
fax = fax(1:nfft/2+1);
%deviding the data into ns-sections with length of nfft
U = reshape(U(1,1:ns*nfft), [nfft, ns])';
%window function
win = hann(nfft,'Periodic')'; %creating a hanning window with nfft values
W = repmat(win,ns,1); %creating "window" matrix to multiply with each section
k=nfft/sum(win,2); %correction coefficient of window function (not sure if correct)
Uw = U.*W; %values of the section multiplied by window function
%Calculation of the frequency spectrum
dim=2; %defining direction for fft()
UF = fft(Uw,nfft,dim);
%Calculating the amplitude
AU = k*abs(UF); %Multiplication with correction coefficient for window function
AU = (1/(ns*nfft))*sum(AU); %averaging the sections
AU = 2*AU(1,1:nfft/2+1); %multiplying the values by 2 --> two spectral lines make up one amplitude
%only the first nfft/2+1 values needed because of multiplication by 2
AU(1) = AU(1)/2;
In my approach I devide the signal into smaller sections. The length of each section depends on the desired frequency resolution. Each section is multiplied by a window function. The spectrum is calculated for each section using the fft function. Then I average the amplitude spectrum of each section.
I know that I lose information if the signal length is not divisible without remainder. My problem is that I am not sure if the results are correct because of the averaging.
I am new to spectral analysis and not very deep into the topic. Hopefully someone can give me an answer. Please let me know if my approach is reasonable. Feel free to point other mistakes.
Best regards
Michael
  2 Comments
Shane L
Shane L on 21 May 2018
Hi Michael, your approach looks reasonable, and your resulting frequency spectrum gives you the proper results based on your specified amplitudes and frequencies in your input signal. You also calculated your correction coefficient correctly. Do you have further questions?
Michael Boy
Michael Boy on 23 May 2018
Hello Shane, thank you very much for your help. At the moment I don't have any further questions. To me spectral analysis is totally new. It is a very interesting topic, but also very hard to understand at the beginning.

Sign in to comment.

Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!