-
Notifications
You must be signed in to change notification settings - Fork 0
/
cool_sound_find_ir.cpp
200 lines (148 loc) · 4.36 KB
/
cool_sound_find_ir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <fftw3.h>
#include <cmath>
#include <iostream>
#include "Buffer.h"
#include "types.h"
#include "libs/config_parser.h"
#include "wav.h"
#include "gnuplot_ipp/gnuplot_ipp.h"
#include "filters.h"
#include "extra.h"
#include <string.h> // memcpy
#include <limits.h>
#include "libs/timer.h"
using std::cout;
using std::endl;
#include <complex>
real complex_norm(real re, real im)
{
return sqrt(re*re + im*im);
}
/// @warn Might not behave well for n=odd!
/* READ!!: http://www.fftw.org/doc/The-Halfcomplex_002dformat-DFT.html */
void HC2magnitude(int N, real *hc, real *magnitude)
{
magnitude[0] = hc[0];
for (idx i=1; i < N/2; ++i)
magnitude[i] = complex_norm(hc[i], hc[N-i]); // Not true for odd N!!!
}
/**
Z = Z1*Z2
@param[in] re1 - Re{Z1}
@param[in] im1 - Im{Z1}
@param[in] re2 - Re{Z2}
@param[in] im2 - Im{Z2}
@param[out] re - Re{Z}
@param[out] im - Im{Z}
*/
inline void complex_multiply(real re1, real im1, real re2, real im2, real *re, real *im)
{
*re = re1*re2 - im1*im2;
*im = re1*im2 + im1*re2;
}
/**
HalfComplex representation multiply
@param[in] z1 - Input HC array
@param[in] z2 - Input HC array
@param[out] z - Output HC array
@param[in] size - Size of the HC array
@warn: ONLY FOR EVEN TRANSFORMATIONS!!!
*/
void hc_multiply (real *z1, real *z2, real *z, idx size)
{
z[0] = z1[0]*z2[0];
idx max_i = size/2;
for (idx i=1; i < max_i; ++i)
complex_multiply(z1[i], z1[size-i],
z2[i], z2[size-i],
&z[i], &z[size-i]);
}
int main(int argc, char **argv)
{
/* Name convention throughout this file:
i - input
o - output
m - magnitude
and capital letters for the frequency domain
*/
Assert(argc >= 5, "Missing program options:\n \tNLMS <desired_wav> <convolved_wav> <output_inverse_impulse_response> <inverse_response_size> [wait]");
SndfileHandle file_d(argv[1]), file_x(argv[2]);
Guarantee(wav::ok(file_d) && wav::ok(file_x), "Impulse response not found.");
Guarantee(wav::mono(file_d) && wav::mono(file_x), "Files must be mono.");
uint sample_rate_Hz = file_d.samplerate();
real T_sampling = 1/(real)sample_rate_Hz;
Guarantee(file_d.samplerate() == file_x.samplerate(), "Sampling rates must match.");
// w-vector are the weights of the adaptive filter and we are initializing them at 1
// File_x is larger than file_d due to the convolution. Set both to the same size.
Buffer<real> d(file_x.frames()), x(file_x.frames()), w(atol(argv[4]), 1.0), y(x.size()+w.size()-1);
file_d.read(d(), file_d.frames());
file_x.read(x(), file_x.frames());
// Plot x-axis buffers
Buffer<real> t(y.size());
for (idx i=0; i < t.size(); ++i)
{
t[i] = i * T_sampling;
//f[i] = i * FFT_df;
}
size_t w_size = w.size();
size_t y_size = y.size();
Buffer<real> e(y), mu(y);
real emu;
Gnuplot pw, py;
pw.set_labels("Coefficients", "Weight value");
py.set_labels("t (s)", "Amplitude");
// Skip initial partial convolution for now
// (implement here)
//
for (size_t n=0; n < w_size-1; ++n)
{
}
for (size_t n=w_size-1; n < y_size; ++n)
{
// This loop performs two operations:
// y[n] = w^T[n] . x[n]
// mu[n] = x^T[n] . x[n]
for (size_t i=0; i < w_size; ++i)
{
y[n] += w[i]*x[n-i];
mu[n] += x[n-i]*x[n-i];
}
e[n] = d[n] - y[n];
emu = e[n] * mu[n];
for (size_t i=0; i < w_size; ++i)
w[i] += emu * x[n-i];
pw.reset();
pw.plot_y(w(),w.size(), "w");
py.reset();
py.plot_xy(t(),y(),y.size(), "y");
cout << 10-w_size << endl;
if (n==10+w_size)
break;
}
// Skip final partial convolution for now
// (implement here)
//
write_mono_wav (argv[3], w(), w.size(), sample_rate_Hz);
/*
ph.set_labels("t (s)", "Amplitude");
ph.plot_xy(t(), h(), FFT_N, "h(t)");
pM.set_labels("f (Hz)", "Magnitude");
pM.cmd("set logscale y");
HC2magnitude(FFT_N, H(), M());
// pM.plot_xy(&f[1], &M[1], FFT_N/2, "|H(f)| AC");
pM.plot_xy(f(), M(), FFT_N/2, "|H(f)|");
*/
/*
HC2magnitude(FFT_N, IH(), MIH());
pMIH.set_labels("f (Hz)", "Magnitude");
pMIH.cmd("set logscale y");
pMIH.plot_xy(f(), MIH(), h_size/2, "|H^{-1}(f)|");
fftw_execute(backwards_plan);
pih.set_labels("t (s)", "Amplitude");
pih.plot_xy(t(), ih(), h_size, "h^{-1}");
*/
if (argc > 5)
wait();
puts("\nSuccess!");
return 0;
}