Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 160 lines (143 sloc) 6.124 kb
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
1 /*************************************************************************
2
3 Copyright 2011 Ibrahim Sha'ath
4
5 This file is part of KeyFinder.
6
7 KeyFinder is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 KeyFinder is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KeyFinder. If not, see <http://www.gnu.org/licenses/>.
19
20 *************************************************************************/
21
8c227e7 Ibrahim Sha'ath Initial commit.
authored
22 #include "downsamplerib.h"
23
24 /*
25 Decent raised cosine filter, designed using A J Fisher's tool at http://www-users.cs.york.ac.uk/~fisher/mkfilter
8ac8737 Ibrahim Sha'ath More defensible LPF coefficients.
authored
26 I usually need a filter that lowpasses under 1760 Hz, with the rolloff complete before the new
27 Nyquist (2205 Hz for a 10x downsample). That's 0.05 of 44100, for the visualiser.
8c227e7 Ibrahim Sha'ath Initial commit.
authored
28
36b3c0b Ibrahim Sha'ath More minor changes to match report.
authored
29 Input to Fisher's tool: 44100, 2000, 0, 161, square root, no comp fn, hamming window.
bff9ba6 Ibrahim Sha'ath General refactoring.
authored
30
8c227e7 Ibrahim Sha'ath Initial commit.
authored
31 IMPORTANT:
32 The filter coefficients are all magic numbers, so this downsampler is only for 44100 fs, 10x ds.
33 */
34
35 class Binode{
36 public:
37 Binode(float x = 0): l(NULL), r(NULL), n(x){}
38 Binode* l, *r;
39 float n;
40 };
41
cb207e5 Ibrahim Sha'ath More semantics
authored
42 AudioStream* PrimaryDownsampler::downsample(AudioStream* instrm, int factor) throw (Exception){
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
43 if(factor == 1) return instrm;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
44 // prep output buffer
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
45 AudioStream* outstrm = new AudioStream();
46 outstrm->setFrameRate(instrm->getFrameRate() / factor);
47 outstrm->setChannels(instrm->getChannels());
48 int c = instrm->getChannels();
49 int ns = instrm->getSampleCount() / factor;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
50 while(ns%c != 0) ns++;
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
51 if(instrm->getSampleCount() % factor > 0) ns += c;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
52 try{
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
53 outstrm->addToSampleCount(ns);
4dbe246 Ibrahim Sha'ath Refactoring
authored
54 }catch(const Exception& e){
55 throw e;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
56 }
36b3c0b Ibrahim Sha'ath More minor changes to match report.
authored
57 // prep filter
58 int filterOrder = 160;
59 float gain = 11.03969310;
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
60 int filterDelay = filterOrder/2;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
61 // create circular buffer for filter delay
62 Binode* p = new Binode(); // first node
63 Binode* q = p;
64 for(int i=0; i<filterOrder; i++){
65 q->r = new Binode(); // subsequent nodes
66 q->r->l = q;
67 q = q->r;
68 }
69 // join first and last nodes
70 p->l = q;
71 q->r = p;
72 // filter coefficients
36b3c0b Ibrahim Sha'ath More minor changes to match report.
authored
73 float b[] = {
8ac8737 Ibrahim Sha'ath More defensible LPF coefficients.
authored
74 // 2000, 0, 161, sqrt, hamming.
36b3c0b Ibrahim Sha'ath More minor changes to match report.
authored
75 -0.0022979864, -0.0014851155, -0.0005276345, +0.0005287637,
76 +0.0016288105, +0.0027066298, +0.0036859262, +0.0044820600,
77 +0.0050064517, +0.0051734225, +0.0049091760, +0.0041622026,
78 +0.0029140060, +0.0011887658, -0.0009395862, -0.0033443515,
79 -0.0058483343, -0.0082321768, -0.0102489292, -0.0116443067,
80 -0.0121813339, -0.0116673677, -0.0099809222, -0.0070953669,
81 -0.0030964983, +0.0018087642, +0.0072947272, +0.0129315999,
82 +0.0182126619, +0.0225928091, +0.0255360681, +0.0265684688,
83 +0.0253317039, +0.0216323992, +0.0154816648, +0.0071199603,
84 -0.0029768131, -0.0141127078, -0.0254095608, -0.0358661777,
85 -0.0444356705, -0.0501157252, -0.0520448654, -0.0495965416,
86 -0.0424622921, -0.0307153754, -0.0148472270, +0.0042291942,
87 +0.0252127139, +0.0464845605, +0.0662137647, +0.0824916099,
88 +0.0934864451, +0.0976077458, +0.0936666466, +0.0810194757,
89 +0.0596811993, +0.0303971839, -0.0053357703, -0.0453047237,
90 -0.0866737087, -0.1261316811, -0.1600878564, -0.1849028543,
91 -0.1971406561, -0.1938239736, -0.1726744703, -0.1323195052,
92 -0.0724487288, +0.0060931437, +0.1012868940, +0.2099971950,
93 +0.3281078087, +0.4507269541, +0.5724509503, +0.6876697384,
94 +0.7908945043, +0.8770856432, +0.9419588972, +0.9822487143,
95 +0.9959106445, +0.9822487143, +0.9419588972, +0.8770856432,
96 +0.7908945043, +0.6876697384, +0.5724509503, +0.4507269541,
97 +0.3281078087, +0.2099971950, +0.1012868940, +0.0060931437,
98 -0.0724487288, -0.1323195052, -0.1726744703, -0.1938239736,
99 -0.1971406561, -0.1849028543, -0.1600878564, -0.1261316811,
100 -0.0866737087, -0.0453047237, -0.0053357703, +0.0303971839,
101 +0.0596811993, +0.0810194757, +0.0936666466, +0.0976077458,
102 +0.0934864451, +0.0824916099, +0.0662137647, +0.0464845605,
103 +0.0252127139, +0.0042291942, -0.0148472270, -0.0307153754,
104 -0.0424622921, -0.0495965416, -0.0520448654, -0.0501157252,
105 -0.0444356705, -0.0358661777, -0.0254095608, -0.0141127078,
106 -0.0029768131, +0.0071199603, +0.0154816648, +0.0216323992,
107 +0.0253317039, +0.0265684688, +0.0255360681, +0.0225928091,
108 +0.0182126619, +0.0129315999, +0.0072947272, +0.0018087642,
109 -0.0030964983, -0.0070953669, -0.0099809222, -0.0116673677,
110 -0.0121813339, -0.0116443067, -0.0102489292, -0.0082321768,
111 -0.0058483343, -0.0033443515, -0.0009395862, +0.0011887658,
112 +0.0029140060, +0.0041622026, +0.0049091760, +0.0051734225,
113 +0.0050064517, +0.0044820600, +0.0036859262, +0.0027066298,
114 +0.0016288105, +0.0005287637, -0.0005276345, -0.0014851155,
115 -0.0022979864
8c227e7 Ibrahim Sha'ath Initial commit.
authored
116 };
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
117
118 // for each channel (should be mono by this point but just in case)
119 for(int i=0; i<c; i++){
8c227e7 Ibrahim Sha'ath Initial commit.
authored
120 q = p;
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
121 // clear delay buffer
122 for(int k=0; k<=filterOrder; k++){
bff9ba6 Ibrahim Sha'ath General refactoring.
authored
123 q->n = 0.0;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
124 q = q->r;
125 }
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
126 // for each frame (running off the end of the file by filterDelay)
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
127 for(int j=i; j<instrm->getSampleCount()+filterDelay; j+=c){
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
128
129 // shuffle old samples along delay buffer
130 p = p->r;
131
132 // load new sample into delay buffer
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
133 if (j < instrm->getSampleCount())
134 p->l->n = instrm->getSample(j) / gain;
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
135 else
136 p->l->n = 0.0; // zero pad once we're into the delay at the end of the file
137
8c227e7 Ibrahim Sha'ath Initial commit.
authored
138 if((j % (factor * c)) < c){ // only do the maths for the useful samples
139 float sum = 0.0;
140 q = p;
141 for(int k=0; k<=filterOrder; k++){
142 sum += b[k] * q->n;
143 q = q->r;
144 }
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
145 // don't try and set samples during the warm-up, only once we've passed filterDelay samples
146 if(j-filterDelay >= 0)
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
147 outstrm->setSample(((j-filterDelay) / factor) + i, sum);
8c227e7 Ibrahim Sha'ath Initial commit.
authored
148 }
149 }
150 }
45c1eb1 Ibrahim Sha'ath Changed main menu policy
authored
151 // delete delay buffer
8c227e7 Ibrahim Sha'ath Initial commit.
authored
152 for(int k=0; k<=filterOrder; k++){
153 q = p;
154 p = p->r;
155 delete q;
156 }
b0b86ef Ibrahim Sha'ath General renaming to match report semantics
authored
157 delete instrm;
158 return outstrm;
8c227e7 Ibrahim Sha'ath Initial commit.
authored
159 }
Something went wrong with that request. Please try again.