Add Russian translation provided by Валерий Крувялис <valkru@mail.ru>
[xiph-mirror.git] / chirptest / window.c
blobd18ad0431cae884ffba7ebfc6c7f8fa6f1bda496
1 /********************************************************************
2 * *
3 * THIS FILE IS PART OF THE OggGhost SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
7 * *
8 * THE OggGhost SOURCE CODE IS (C) COPYRIGHT 2007-2011 *
9 * by the Xiph.Org Foundation http://www.xiph.org/ *
10 * *
11 ********************************************************************
13 function: window functions for research code
14 last mod: $Id$
16 ********************************************************************/
18 #define _GNU_SOURCE
19 #include <math.h>
20 #include "window.h"
22 /* A few windows */
24 /* Not-a-window */
25 static void rectangle(float *x, int n){
26 int i;
27 for(i=0;i<n;i++)
28 x[i]=1.;
31 /* sine window */
32 static void sine(float *x, int n){
33 int i;
34 float scale = M_PI/n;
36 for(i=0;i<n;i++){
37 float i5 = i+.5;
38 x[i] = sin(scale*i5);
42 /* Minimum 4-term Blackman Harris; highest sidelobe = -92dB */
43 #define A0 .35875f
44 #define A1 .48829f
45 #define A2 .14128f
46 #define A3 .01168f
48 static void blackmann_harris(float *x, int n){
49 int i;
50 float scale = 2*M_PI/n;
52 for(i=0;i<n;i++){
53 float i5 = i+.5;
54 float w = A0 - A1*cos(scale*i5) + A2*cos(scale*i5*2) - A3*cos(scale*i5*3);
55 x[i] = w;
59 /* Good 'ol Hanning window (narrow mainlobe, fast falloff, high sidelobes) */
60 static void hanning(float *x, int n){
61 int i;
62 float scale = 2*M_PI/n;
64 for(i=0;i<n;i++){
65 float i5 = i+.5;
66 x[i] = (.5-.5*cos(scale*i5));
70 /* Triangular * gaussian (sidelobeless window) */
71 #define TGA 1.e-4
72 #define TGB 21.6
73 static void tgauss_deep(float *x, int n){
74 int i;
75 for(i=0;i<n;i++){
76 float f = (i+.5-n/2.)/(n/2.);
77 x[i] = exp(-TGB*pow(f,2)) * pow(1.-fabs(f),TGA);
81 static void vorbis(float *d, int n){
82 int i;
83 for(i=0;i<n;i++)
84 d[i] = sin(0.5 * M_PI * sin((i+.5)/n * M_PI)*sin((i+.5)/n * M_PI));
87 static float beta(int n, float alpha){
88 return cosh (acosh(pow(10,alpha))/(n-1));
91 static double T(double n, double x){
92 if(fabs(x)<=1){
93 return cos(n*acos(x));
94 }else{
95 return cosh(n*acosh(x));
99 /* Dolph-Chebyshev window (a=6., all sidelobes < -120dB) */
100 static void dolphcheb(float *d, int n){
101 int i,k;
102 float a = 6.;
103 int M=n/2;
104 int N=M*2;
105 double b = beta(N,a);
107 for(i=0;i<n;i++){
108 double sum=0;
109 for(k=0;k<M;k++)
110 sum += (k&1?-1:1)*T(N,b*cos(M_PI*k/N)) * cos (2*i*k*M_PI/N);
111 sum /= T(N,b);
112 sum-=.5;
113 d[i]=sum;
117 /* sidelobeless window machine optimized for fast convergence */
118 static void maxwell1(float *x, int n){
119 int i;
120 float scale = 2*M_PI/n;
121 for(i=0;i<n;i++){
122 float i5 = i+.5;
123 x[i] = pow( 119.72981
124 - 119.24098*cos(scale*i5)
125 + 0.10283622*cos(2*scale*i5)
126 + 0.044013144*cos(3*scale*i5)
127 + 0.97203713*cos(4*scale*i5),
128 1.9730763)/50000.;
132 window_bundle window_functions = {
133 rectangle,
134 sine,
135 hanning,
136 vorbis,
137 blackmann_harris,
138 tgauss_deep,
139 dolphcheb,
140 maxwell1,