1 /*
2   lpred.h:
3 
4   Copyright 2020 Victor Lazzarini
5 
6   streaming linear prediction
7 
8   This file is part of Csound.
9 
10   The Csound Library is free software; you can redistribute it
11   and/or modify it under the terms of the GNU Lesser General Public
12   License as published by the Free Software Foundation; either
13   version 2.1 of the License, or (at your option) any later version.
14 
15   Csound is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU Lesser General Public License for more details.
19 
20   You should have received a copy of the GNU Lesser General Public
21   License along with Csound; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23   02110-1301 USA
24 */
25 
26 #ifndef CSOUND_LPRED_H
27 #define CSOUND_LPRED_H
28 
29 #if !defined(__BUILDING_LIBCSOUND)
30 #  error "Csound plugins and host applications should not include lpred.h"
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "pstream.h"
38 
39   typedef struct _mycmplx {
40     MYFLT re; MYFLT im;
41   } MYCMPLX;
42 
43   /**
44    * Compute autocorrelation function
45    * r: autocorrelation output array (size N)
46    * s: input signal
47    * N: signal size
48    * returns: autocorrelation r
49    */
50   MYFLT *csoundAutoCorrelation(CSOUND *csound, MYFLT *r, MYFLT *s, int N);
51 
52 
53   /**
54    * Linear prediction setup
55    *
56    * N: autocorrelation size
57    * M: filter order
58    *
59    * returns: opaque LP structure to use with linear prediction function
60    */
61   void *csoundLPsetup(CSOUND *csound, int N, int M);
62 
63   /**
64    * Linear prediction setup deallocation
65    *
66    * param: LP setup object
67    *
68    */
69 
70   void csoundLPfree(CSOUND *csound, void *param);
71 
72   /**
73    * Compute linear prediction coefficients
74    *
75    * x: input signal
76    * param: LP setup object
77    *
78    * returns: array of size M+1 with error E and coefficients 1-M
79    * output format is [E,c1,c2,...,cm] OR NULL if a memory problem occured
80    * NB: c0 is always 1
81    */
82   MYFLT *csoundLPred(CSOUND *csound, void *p, MYFLT *x);
83 
84   /**
85    * Compute cepstrum coefficients from all-pole coefficients
86    * and linear prediction error
87    *
88    * c: array of size N
89    * b: array of size M+1 with M all-pole coefficients
90    *   and E in place of coefficient 0 [E,c1,...,cM]
91    * N: size of cepstrum array output
92    * M: all-pole filter order
93    *
94    * returns: array with N cepstrum coefficients
95    * NB: cepstrum is computed from power spectrum
96    */
97   MYFLT *csoundCepsLP(CSOUND *csound, MYFLT *b, MYFLT *c, int M, int N);
98 
99   /**
100    * Compute all-pole coefficients and linear prediction error
101    * from cepstrum coefficients
102    *
103    * b: array of size M+1
104    * c: array of size N with cepstrum coeffs
105    *
106    * M: all-pole filter order
107    * N: cepstrum size
108    *
109    * returns: M+1 size array with all-pole coefficients 1-M and
110    * E in place of coefficient 0 [E,c1,...,cM]
111    * NB: cepstrum is expected to be computed from power spectrum
112    */
113   MYFLT *csoundLPCeps(CSOUND *csound, MYFLT *c, MYFLT *b, int N, int M);
114 
115   /**
116    * Returns the computed RMS from LP object
117    */
118   MYFLT csoundLPrms(CSOUND *csound, void *parm);
119 
120 
121   typedef struct _lpfil {
122     OPDS h;
123     MYFLT *out;
124     MYFLT *in, *koff, *kflag, *ifn, *isiz, *iord, *iwin;
125     AUXCH coefs;
126     AUXCH del;
127     AUXCH buf;
128     int32_t M, N, wlen;
129     int32_t rp;
130     void *setup;
131     MYFLT *win, g;
132     FUNC *ft;
133   } LPCFIL;
134 
135   typedef struct _lpfil2 {
136     OPDS h;
137     MYFLT *out;
138     MYFLT *in, *sig, *flag, *prd, *isiz, *iord, *iwin;
139     AUXCH coefs;
140     AUXCH del;
141     AUXCH buf;
142     AUXCH cbuf;
143     int32_t M, N, wlen;
144     int32_t rp,bp,cp;
145     MYFLT *win, g;
146     void *setup;
147   } LPCFIL2;
148 
149   typedef struct _lpreda {
150     OPDS h;
151     ARRAYDAT *out;
152     MYFLT *rms, *err, *cps;
153     MYFLT  *off, *flag, *ifn, *isiz, *iord, *iwin;
154     AUXCH buf;
155     int32_t M, N, wlen;
156     FUNC *ft;
157     MYFLT *win;
158     void *setup;
159   } LPREDA;
160 
161   typedef struct _lpfil3 {
162     OPDS h;
163     MYFLT *out, *in;
164     ARRAYDAT *coefs;
165     AUXCH del;
166     int32_t M;
167     int32_t rp;
168     void *setup;
169   } LPCFIL3;
170 
171   typedef struct _lpreda2 {
172     OPDS h;
173     ARRAYDAT *out;
174     MYFLT *rms, *err, *cps;
175     MYFLT  *in, *flag, *prd, *isiz, *iord, *iwin;
176     AUXCH cbuf;
177     AUXCH buf;
178     int32_t M, N, wlen, cp, bp;
179     MYFLT *win;
180     void *setup;
181   } LPREDA2;
182 
183 
184   typedef struct _lpreda3 {
185     OPDS h;
186     PVSDAT *fout;
187     MYFLT  *in, *isiz, *prd, *iord, *iwin;
188     AUXCH cbuf;
189     AUXCH buf;
190     AUXCH fftframe;
191     int32_t M, N, wlen, cp, bp;
192     MYFLT *win;
193     void *setup;
194   } LPCPVS;
195 
196 
197   typedef struct _pvscoefs {
198     OPDS h;
199     ARRAYDAT *out;
200     MYFLT *krms, *kerr;
201     PVSDAT  *fin;
202     MYFLT  *iord, *imod;
203     AUXCH coef;
204     AUXCH buf;
205     int32_t M, N;
206     MYFLT rms;
207     MYFLT err;
208     MYFLT mod;
209     uint32_t framecount;
210     void *setup;
211   } PVSCFS;
212 
213 
214   typedef struct _cf2p {
215     OPDS h;
216     ARRAYDAT *out;
217     ARRAYDAT *in;
218     int32_t M;
219     void *setup;
220     MYFLT sum;
221   } CF2P;
222 
223   typedef struct {
224     OPDS    h;
225     MYFLT   *ar, *asig;
226     ARRAYDAT *kparm;
227     MYFLT   *kmin, *kmax, *iprd, *imod, *iscl, *istor;
228     int     scale, ord;
229     AUXCH   y1m,y2m,y1o,y2o,y1c,y2c;
230     MYFLT kcnt;
231   } RESONB;
232 
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif      /* CSOUND_LPRED_H */
239