How can I fit a second Fourier component to a polar histogram?

5 views (last 30 days)
I would like to fit a second Fourier series function: E(x) = (1/2*pi)*(1+A1*cos(2*xdata-A2))
to
theta = atan2(rand(100000,1)-0.5,2*(rand(100000,1)-0.5)); polarhistogram(theta,25);
Regards, Eric

Answers (1)

David Goodmanson
David Goodmanson on 24 May 2017
Hi Eric, see how this works. I added an adjustable tilt angle to the random data to test the fit. The code compares the fit to the unnormalized histogram, with its total of 1e5 points. To go to the normalized expression you have, then A1 = B1/c(n0) and A2 = -B2.
npts = 1e5;
n = 25; % should be odd
tilt = pi/4;
theta = atan2(rand(npts,1)-0.5,2*(rand(npts,1)-0.5)) + tilt;
theta = mod(theta+pi,2*pi)-pi;
h = polarhistogram(theta,n);
% start fit
val = h.Values;
c = fftshift(fft(ifftshift(val)))/n; % fourier coefficients
% n0 is index for constant term. c(n0) = npts/n = average bin value
n0 = (n+1)/2;
B1 = 2*abs(c(n0+2));
B2 = angle(c(n0+2));
theta1 = (h.BinEdges(1:end-1) + h.BinEdges(2:end))/2; % bin centers
E = c(n0) + B1.*cos(2*theta1 + B2);
hold on
polarplot(theta1,E,'-o')
hold off
  3 Comments
Eric Gbadam
Eric Gbadam on 26 May 2017
David, I get this error message anytime I run the code
Undefined function or variable 'polarhistogram'.
Error in polarfit (line 6) h = polarhistogram(theta,n);
David Goodmanson
David Goodmanson on 26 May 2017
Hi Eric, what version of matlab are you using? This seems a bit odd since your original question contains that function.

Sign in to comment.

Categories

Find more on Networks 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!