Sliding window for polynomial fitting of a function

9 views (last 30 days)
I have a function that I would like to assess using a sliding window to fit a polynomial. Ideally this routine would have the following characteristics:
  • Function is Pn (below)
  • Need to calculate a polynomial fit within a sliding window (see poly_order)
  • Output should be the y-value of the fit calculated at the midpoint of the window, iterated over each window (see poly_fit) stored as an vector for future analyses
I am assuming this will need to be reformatted into a loop of some sort perhaps, but I am puzzled about how to make this happen. If I can better clarify this request, please let me know. Thanks, in advance.
Here is my code thus far:
% Define Constants
po = 0.009982; % Density of water, g/m3
f = 650000; % Frequency, in Hz
c = 1481; % Speed of sound, m/s
Uo = 0.1; % Max velocity of source surface
w = 2*pi*f; % Angular frequency of source acoustic field
k = w/c; % Acoustic wave number
a = 0.005; % Radius of source, in m
r = 10*a; % Distance from center of source
% Define Array and Formulas
theta = (-pi/2:0.0001:pi/2)'; % Time step array for discretization,'x-vector'
A = 0.5*po*c*Uo*(a/r)*(k*a); % Amplitude of pressure fluctuation at distance r along centerline
J1 = besselj(1,k*a*sin(theta)); % Bessel function of the first kind, order 1
H_theta = abs(2*J1./(k*a*sin(theta))); % Shape function
% Pressure function
Pn = A.*H_theta;
%Polynomial fitting to Pressure Function
poly_order = polyfit(theta,Pn,20) % Determines coeffients of best fit equation
poly_fit = polyval(poly_order,theta); % y-values of poly fit at different values of theta (x)

Answers (1)

TADA
TADA on 31 Mar 2019
Edited: TADA on 31 Mar 2019
What you describe is a stavisky golay filter if I understand correctly
windowSize = 10;
polyOrder = 20;
poly_fit = smooth(theta,Pn,windowSize,'sgolay',polyOrder);

Categories

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