Simulink Matlab Fcn, logical error for incrementation in Goertzel's algorithm
Ältere Kommentare anzeigen
I am trying to use the Simulink Fcn block to code out Goertzel's algorithm to implement digital fourier transform(DFT) in real time for my project. The target of the DFT is the grid current, i implemented the code below based on the abc coordinates. However, I found this error where the variable count should increment by time, upto 200 and then reset back to zero, but during the simulation count just remains as 1, what could be the problem?? shouldn't count=count+1; increment over time?
Additionally, if there is anybody familiar with Goertzel's algorithm, that would also help!
function [mag_a,mag_b,mag_c,count]= goertzel(Ia,Ib,Ic,h_freq)
%%Calculate magnitude of harmonic h (e.g., h=5 for 250Hz @ 50Hz grid)
%initialization------
s0_a=double(0);
s1_a=double(0);
s2_a=double(0);
s0_b=double(0);
s1_b=double(0);
s2_b=double(0);
s0_c=double(0);
s1_c=double(0);
s2_c=double(0);
real_a=double(0);
imag_a=double(0);
mag_a=double(0);
real_b=double(0);
imag_b=double(0);
mag_b=double(0);
real_c=double(0);
imag_c=double(0);
mag_c=double(0);
last_mag_a=double(0);
last_mag_b=double(0);
last_mag_c=double(0);
count=single(0);
bin=uint(0) ; %integer, k
k=uint(0);
%--------------------
fs=1000;
N=100;
k=0.5+(N*h_freq)/fs;
omega=(2*pi*k)/N;
%calculation of Goertzel Algorithm for abc coordinates
%to find magnitude in individual harmonic orders
s0_a = Ia + 2*cos(omega)*s1_a-s2_a;
s2_a = s1_a;
s1_a = s0_a;
s0_b = Ib + 2*cos(omega)*s1_b-s2_b;
s2_b = s1_b;
s1_b = s0_b;
s0_c = Ic + 2*cos(omega)*s1_c-s2_c;
s2_c = s1_c;
s1_c = s0_c;
count=count+1;
if count == N
real_a = s1_a-s2_a*cos(omega);
imag_a = s2_a*sin(omega);
last_mag_a = 2*sqrt(real_a*real_a+imag_a*imag_a)/N;
real_b = s1_b-s2_b*cos(omega);
imag_b = s2_b*sin(omega);
last_mag_b = 2*sqrt(real_b*real_b+imag_b*imag_b)/N;
real_c = s1_c-s2_c*cos(omega);
imag_c = s2_c*sin(omega);
last_mag_c = 2*sqrt(real_c*real_c+imag_c*imag_c)/N;
count=0;
s1_a=0;
s2_a=0;
s1_b=0;
s2_b=0;
s1_c=0;
s2_c=0;
end

Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 18 Jun. 2025
count=single(0);
That statement is executed every time the block is executed.
You need
persistent count
if isempty(count); count = single(0); end
1 Kommentar
bieron surya
am 18 Jun. 2025
Kategorien
Mehr zu Get Started with DSP System Toolbox finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!