xue@11: /* xue@11: Harmonic sinusoidal modelling and tools xue@11: xue@11: C++ code package for harmonic sinusoidal modelling and relevant signal processing. xue@11: Centre for Digital Music, Queen Mary, University of London. xue@11: This file copyright 2011 Wen Xue. xue@11: xue@11: This program is free software; you can redistribute it and/or xue@11: modify it under the terms of the GNU General Public License as xue@11: published by the Free Software Foundation; either version 2 of the xue@11: License, or (at your option) any later version. xue@11: */ xue@1: //--------------------------------------------------------------------------- xue@1: xue@1: #include "splines.h" xue@1: Chris@5: /** \file splines.h */ Chris@5: xue@1: //--------------------------------------------------------------------------- Chris@5: /** xue@1: function tridiagonal: solves linear system A[N][N]x[N]=d[N] where A is tridiagonal xue@1: xue@1: In: tridiagonal matrix A[N][N] gives as three vectors - lower subdiagonal xue@1: a[1:N-1], main diagonal b[0:N-1], upper subdiagonal c[0:N-2] xue@1: vector d[N] xue@1: Out: vector d[N] now containing x[N] xue@1: xue@1: No return value. xue@1: */ xue@1: void tridiagonal(int N, double* a, double* b, double* c, double* d) xue@1: { xue@1: for (int k=1; k=0; k--) xue@1: d[k]=(d[k]-c[k]*d[k+1])/b[k]; xue@1: }//tridiagonal xue@1: Chris@5: /** xue@1: function CubicSpline: computing piece-wise trinomial coefficients of a cubic spline xue@1: xue@1: In: x[N+1], y[N+1]: knots xue@1: bordermode: boundary mode of cubic spline xue@1: bordermode=0: natural: y''(x0)=y''(x_N)=0 xue@1: bordermode=1: quadratic runout: y''(x0)=y''(x1), y''(x_N)=y''(x_(N-1)) xue@1: bordermode=2: cubic runout: y''(x0)=2y''(x1)-y''(x2), the same at the end point xue@1: mode: spline format xue@1: mode=0: spline defined on the l'th piece as y(x)=a[l]x^3+b[l]x^2+c[l]x+d[l] xue@1: mode=1: spline defined on the l'th piece as y(x)=a[l]t^3+b[l]t^2+c[l]t+d[l], t=x-x[l] xue@1: Out: a[N],b[N],c[N],d[N]: piecewise trinomial coefficients xue@1: data[]: cubic spline computed for [xstart:xinterval:xend], if specified xue@1: No return value. On start d must be allocated no less than N+1 storage units. xue@1: */ xue@1: void CubicSpline(int N, double* a, double* b, double* c, double* d, double* x, double* y, int bordermode, int mode, double* data, double xinterval, double xstart, double xend) xue@1: { xue@1: double *h=new double[N]; xue@1: for (int i=0; ix[n+1]) n++; xue@1: if (mode==1) xx=xx-x[n]; xue@1: data[i]=d[n]+xx*(c[n]+xx*(b[n]+xx*a[n])); xue@1: } xue@1: } xue@1: }//CubicSpline xue@1: Chris@5: /** xue@1: function CubicSpline: computing piece-wise trinomial coefficients of a cubic spline with uniformly placed knots xue@1: xue@1: In: y[N+1]: spline values at knots (0, h, 2h, ..., Nh) xue@1: bordermode: boundary mode of cubic spline xue@1: bordermode=0: natural: y''(x0)=y''(x_N)=0 xue@1: bordermode=1: quadratic runout: y''(x0)=y''(x1), y''(x_N)=y''(x_(N-1)) xue@1: bordermode=2: cubic runout: y''(x0)=2y''(x1)-y''(x2), the same at the end point xue@1: mode: spline format xue@1: mode=0: spline defined on the l'th piece as y(x)=a[l]x^3+b[l]x^2+c[l]x+d[l] xue@1: mode=1: spline defined on the l'th piece as y(x)=a[l]t^3+b[l]t^2+c[l]t+d[l], t=x-x[l] xue@1: Out: a[N],b[N],c[N],d[N]: piecewise trinomial coefficients xue@1: data[]: cubic spline computed for [xstart:xinterval:xend], if specified xue@1: No return value. On start d must be allocated no less than N+1 storage units. xue@1: */ xue@1: void CubicSpline(int N, double* a, double* b, double* c, double* d, double h, double* y, int bordermode, int mode, double* data, double xinterval, double xstart, double xend) xue@1: { xue@1: for (int i=0; i(n+1)*h) n++; xue@1: if (mode==1) xx=xx-n*h; xue@1: data[i]=d[n]+xx*(c[n]+xx*(b[n]+xx*a[n])); xue@1: } xue@1: } xue@1: }//CubicSpline xue@1: Chris@5: /** xue@1: function Smooth_Interpolate: smoothly interpolate/extrapolate P-piece spline from P-1 values. The xue@1: interpolation scheme is chosen according to P: xue@1: P-1=1: constant xue@1: P-1=2: linear function xue@1: P-1=3: quadratic function xue@1: P-1>=4: cubic spline xue@1: xue@1: In: xind[P-1], x[P-1]: knot points xue@1: Out: y[N]: smoothly interpolated signal, y(xind[p])=x[p], p=0, ..., P-2. xue@1: xue@1: No return value. xue@1: */ xue@1: void Smooth_Interpolate(double* y, int N, int P, double* x, double* xind) xue@1: { xue@1: if (P==2) xue@1: { xue@1: for (int n=0; n