/* model.c -- program to help test DSP filter and rate-conversion code. Copyright (C) 1999 Stanley J. Brooks This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include // for open,read,write: #include #include #include #include #include #include #include #include #define FLOAT double #ifndef LSAMPL # define SAMPL short # define MAXSAMPL 0x7fff # define MINSAMPL -0x8000 #else # define SAMPL long # define MAXSAMPL 0x7fffff # define MINSAMPL -0x800000 #endif static struct _Env { int r; /* rise */ int c; /* center */ int f; /* fall */ double v; /* volume */ double d; /* decay */ } Env = {0,0,0,0.5,1.0}; static void Usage(void)__attribute__((noreturn)); static void Usage(void) { fprintf(stderr,"Usage: ./model rate0,rate1 [-l len] [-f freq] \n"); exit(-1); } static int ReadN(int fd, SAMPL *v, int n) { int r; do { r = read(fd, (char*)(v), n*sizeof(SAMPL)); }while(r==-1 && errno==EINTR); if (r==-1 || r%sizeof(SAMPL)) { perror("Error reading input"); exit(-1); } return r/sizeof(SAMPL); } #define BSIZ 400000/*0x10000*/ static double bestfit(double sx, double sy, double h11, double h12, double h22, double *cx, double *cy) { double a,su,uu,cu,w2; a=0; *cy=0; if (h22>1e-20*h11) { a = h12/h22; /* u[] = x[] - a*y[] is orthogonal to y[] */ *cy = sy/h22; /* *cy*y[] is projection of s[] onto y[] */ } /* = sx -a*sy */ su = sx - a*sy; /* su is iprod of s[] and u[] */ /* = = h11 - 2a*h11 + a*a*h22 */ uu = h11 - 2*a*h12 + a*a*h22; cu = 0; if (uu>1e-20*h11) cu = su/uu; /* s[] = *cy * y[] + cu * u[] is orthogonal decomposition */ w2 = *cy * *cy * h22; w2 += cu * cu * uu; *cx = cu; *cy -= a*cu; return w2; } static double eCenter(const SAMPL *ibuff, int len) { double v0,v1,v2; int n; v0 = v1 = v2 = 0; for (n=0; n