- #1
1Keenan
- 101
- 4
Hi all,
I have a problem in evaluating Fourier coefficient.
I have an array of numbers (which is a magnetic field) taken on a circle of radius 8mm and I want to know the harmonics of this field.
I have written a code in matlab, I have used some in built function in MATLAB and mathcad, but I can get the correct results.
Any advise?
my code is:
I have a problem in evaluating Fourier coefficient.
I have an array of numbers (which is a magnetic field) taken on a circle of radius 8mm and I want to know the harmonics of this field.
I have written a code in matlab, I have used some in built function in MATLAB and mathcad, but I can get the correct results.
Any advise?
my code is:
Code:
x=Bx(:,4);
passo=length(x);
period = 2*pi ;
t=linspace(0,period,passo);
t=t';
Tc=t(2)-t(1);
T = max(t)-min(t);
C = 2/T;
omega = (2*pi/T);
n_armoniche =16;
a0 = C*sum(x)*Tc;
for k=1:n_armoniche
a(k)=C*sum(x.*cos(omega*k.*t))*Tc;
b(k)=C*sum(x.*sin(omega*k.*t))*Tc;
c(k)= sqrt(a(k)^2+b(k)^2);
phi(k)=atand(b(k)/a(k));
end
xvalues = a0/2*ones(length(t),1);
for kval=1:n_armoniche
xvalues=xvalues+a(kval)*cos(omega*kval.*t);
xvalues=xvalues+b(kval)*sin(omega*kval.*t);
end
figure()
subplot(3,1,1)
plot(t,x, 'Linewidth', 2)
hold on
plot(t,xvalues,'r','Linewidth', 2)
xlabel('t')
ylabel('function')
axis([min(t) max(t) -inf inf])
subplot(3,1,2)
plot(t, a0/2*ones(1,length(t)), 'Linewidth', 2)
hold all
for kval = 1:n_armoniche
plot(t,a(kval)*cos(omega*kval.*t), 'Linewidth', 2)
plot(t,b(kval)*cos(omega*kval.*t), 'Linewidth', 2)
end
xlabel('t')
ylabel('function')
title('Armoniche')
grid on
legend('a0','a1','b1','a2','b2','a3','b3','a4','b4','...')
axis([min(t) max(t) -inf inf])
subplot(3,1,3)
plot(t, a0/2*ones(1,length(t)), 'Linewidth', 2)
hold all
for kval = 1:n_armoniche
plot(t,c(kval)*cos(omega*kval.*t+phi(kval)), 'Linewidth', 2)
end
xlabel('t')
ylabel('function')
title('Armoniche in forma polare')
grid on
legend('c0','c1','c2','c3','c4','c5','c6','c7','c8','...')
axis([min(t) max(t) -inf inf])