17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

-----What follows is the .c version of toepsolve--------<br />

#include <br />

#include "mex.h"<br />

/* function hinv = toepsolve(r,q);<br />

* TOEPSOLVE Solve Toeplitz system of equations.<br />

* Solves R*hinv = q, where R is the symmetric Toeplitz matrix<br />

* whos first column is r<br />

* Assumes all inputs are real<br />

* Inputs:<br />

* r - first column of Toeplitz matrix, length n<br />

* q - rhs vector, length n<br />

* Outputs:<br />

* hinv - length n solution<br />

*<br />

* Algorithm from Roberts & Mullis, p.233<br />

*<br />

* Author: T. Krauss, Sept 10, 1997<br />

*<br />

* Modified: R. Cain, Dec 16, 2004 to replace unnecessasary<br />

* n by n matrix allocation for a with an n by 2 rotating<br />

* buffer and to more closely match the .m function.<br />

*/<br />

void mexFunction(<br />

int nlhs,<br />

mxArray *plhs[],<br />

int nrhs,<br />

const mxArray *prhs[]<br />

)<br />

{<br />

double (*a)[2],*hinv,alpha,beta;<br />

int c,d,temp,j,k;<br />

}<br />

double eps = mxGetEps();<br />

int n = (mxGetN(prhs[0])>=mxGetM(prhs[0])) ? mxGetN(prhs[0]) : mxGetM(prhs[0]) ;<br />

double *r = mxGetPr(prhs[0]);<br />

double *q = mxGetPr(prhs[1]);<br />

a = (double (*)[2])mxCalloc((n+1)*2,sizeof(double));<br />

if (a == NULL) {<br />

mexErrMsgTxt("Sorry, failed to allocate buffer.");<br />

}<br />

a[0][0]=1.0;<br />

plhs[0] = mxCreateDoubleMatrix(n,1,0);<br />

hinv = mxGetPr(plhs[0]);<br />

hinv[0] = q[0]/r[0];<br />

alpha=r[0];<br />

c=0;<br />

d=1;<br />

for (k = 1; k < n; k++) {<br />

a[k][c] = 0;<br />

a[0][d] = 1.0;<br />

beta = 0.0;<br />

for (j = 1; j

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!