Repository navigation
Expand file tree
/
Copy pathbafflechamferresponse.cpp
More file actions
433 lines (389 loc) · 18 KB
/
Copy pathbafflechamferresponse.cpp
File metadata and controls
433 lines (389 loc) · 18 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* KFilter6
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright (C) 2002-2026 Martin Erdtmann
*/
#include "bafflechamferresponse.h"
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstddef>
#include <limits>
#include <vector>
namespace
{
constexpr double Pi = 3.141592653589793238462643383279502884;
constexpr double TwoPi = 2.0 * Pi;
constexpr double SpeedOfSoundMPerS = 343.0;
constexpr double ChamferNu = 4.0 / 5.0;
constexpr double ChamferOpenAngleRad = 5.0 * Pi / 4.0;
constexpr std::size_t FirstOrderElementCount = 240;
constexpr std::size_t MinimumHigherOrderElementCount = 64;
constexpr std::size_t MaximumHigherOrderElementCount = 120;
constexpr double HigherOrderHeightSetbackFactor = 0.6;
bool finiteComplex(const std::complex<double>& value)
{
return std::isfinite(value.real()) && std::isfinite(value.imag());
}
std::size_t pairIndex(std::size_t first, std::size_t second, std::size_t count)
{
return first * count + second;
}
std::size_t tripleIndex(std::size_t first,
std::size_t second,
std::size_t third,
std::size_t count)
{
return (first * count + second) * count + third;
}
std::size_t higherOrderElementCount(double edgeHeightM, double setbackM)
{
const double requested = std::ceil(HigherOrderHeightSetbackFactor * edgeHeightM / setbackM);
if (!std::isfinite(requested)) {
return MaximumHigherOrderElementCount;
}
const double bounded = std::clamp(
requested,
static_cast<double>(MinimumHigherOrderElementCount),
static_cast<double>(MaximumHigherOrderElementCount));
return static_cast<std::size_t>(bounded);
}
bool calculateFirstOrderWedge(std::array<std::complex<double>, KFilterFrequencyCount>& response,
double perpendicularSourceDistanceM,
double sourceYM,
double edgeHeightM,
double closedWedgeAngleRad)
{
if (!std::isfinite(perpendicularSourceDistanceM) ||
!std::isfinite(sourceYM) ||
!std::isfinite(edgeHeightM) ||
!std::isfinite(closedWedgeAngleRad) ||
perpendicularSourceDistanceM <= 0.0 ||
edgeHeightM <= 0.0) {
return false;
}
const double acousticOpenAngle = TwoPi - closedWedgeAngleRad;
const double nu = Pi / acousticOpenAngle;
if (!std::isfinite(nu) || nu <= 0.0) {
return false;
}
const double dy = edgeHeightM / static_cast<double>(FirstOrderElementCount);
std::vector<double> amplitudes(FirstOrderElementCount);
std::vector<double> pathExtras(FirstOrderElementCount);
const double angleA = 1.5 * Pi * nu;
const double angleB = 0.5 * Pi * nu;
const double sinA = std::sin(angleA);
const double cosA = std::cos(angleA);
const double sinB = std::sin(angleB);
const double cosB = std::cos(angleB);
for (std::size_t index = 0; index < FirstOrderElementCount; ++index) {
const double y = (static_cast<double>(index) + 0.5) * dy;
const double z = y - sourceYM;
const double sourceToEdge = std::hypot(perpendicularSourceDistanceM, z);
// Analytic on-axis far-field limit. The common 1/R pressure factor is
// removed because C123 is a ratio of edge contributions; no arbitrary
// observer distance enters the production transfer.
const double raw = std::max(1.0, sourceToEdge / perpendicularSourceDistanceM);
const double coshNuEta = std::cosh(nu * std::acosh(raw));
const double denominatorA = coshNuEta - cosA;
const double denominatorB = coshNuEta - cosB;
if (!std::isfinite(denominatorA) || !std::isfinite(denominatorB) ||
std::abs(denominatorA) <= std::numeric_limits<double>::epsilon() ||
std::abs(denominatorB) <= std::numeric_limits<double>::epsilon()) {
return false;
}
// theta_s = 0 and theta_r = pi/2 make terms 1/3 and 2/4 equal.
const double beta = 2.0 * sinA / denominatorA + 2.0 * sinB / denominatorB;
amplitudes[index] = beta / sourceToEdge * dy * (-nu / (4.0 * Pi));
pathExtras[index] = sourceToEdge;
if (!std::isfinite(amplitudes[index]) || !std::isfinite(pathExtras[index])) {
return false;
}
}
const KFilterFrequencyGrid& frequencies = kfilterFrequencyGridHz();
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double waveNumber = TwoPi * frequencies[sampleIndex] / SpeedOfSoundMPerS;
std::complex<double> value{0.0, 0.0};
for (std::size_t index = 0; index < FirstOrderElementCount; ++index) {
const double phase = -waveNumber * pathExtras[index];
value += amplitudes[index] * std::complex<double>{std::cos(phase), std::sin(phase)};
}
if (!finiteComplex(value)) {
return false;
}
response[sampleIndex] = value;
}
return true;
}
class HigherOrderChamferKernel
{
public:
HigherOrderChamferKernel(double distanceFromOriginalEdgeM,
double sourceYM,
double edgeHeightM,
double setbackM)
: m_count(higherOrderElementCount(edgeHeightM, setbackM)),
m_setbackM(setbackM),
m_distanceFromOriginalEdgeM(distanceFromOriginalEdgeM),
m_sourceYM(sourceYM),
m_edgeHeightM(edgeHeightM),
m_edgeSeparationM(std::sqrt(2.0) * setbackM),
m_dy(edgeHeightM / static_cast<double>(m_count)),
m_c2(std::pow(ChamferNu * m_dy, 2.0) / (4.0 * Pi * Pi)),
m_c3(m_c2 * (-ChamferNu * m_dy / (2.0 * Pi)))
{
}
bool prepare()
{
if (!std::isfinite(m_setbackM) || !std::isfinite(m_distanceFromOriginalEdgeM) ||
!std::isfinite(m_sourceYM) || !std::isfinite(m_edgeHeightM) ||
m_setbackM <= 0.0 || m_edgeHeightM <= 0.0 ||
m_distanceFromOriginalEdgeM <= m_setbackM ||
m_count < MinimumHigherOrderElementCount ||
m_count > MaximumHigherOrderElementCount) {
return false;
}
m_y.resize(m_count);
for (std::size_t index = 0; index < m_count; ++index) {
m_y[index] = (static_cast<double>(index) + 0.5) * m_dy;
}
const std::size_t pairCount = m_count * m_count;
const std::size_t tripleCount = pairCount * m_count;
m_sourceGeometry.resize(pairCount);
m_sourcePath.resize(pairCount);
m_intermediateGeometry.resize(tripleCount);
m_edgePath.resize(pairCount);
m_receiver2Geometry.resize(pairCount);
m_receiver2Path.resize(pairCount);
m_receiver1Geometry.resize(pairCount);
m_receiver1Path.resize(pairCount);
const double sourceRadius = m_distanceFromOriginalEdgeM - m_setbackM;
const double sourceAngle = ChamferNu * (Pi + ChamferOpenAngleRad);
const double sourceAngleSin = std::sin(sourceAngle);
const double sourceAngleCos = std::cos(sourceAngle);
for (std::size_t first = 0; first < m_count; ++first) {
for (std::size_t second = 0; second < m_count; ++second) {
const std::size_t index = pairIndex(first, second, m_count);
const double sourceToFirst =
std::hypot(sourceRadius, m_y[first] - m_sourceYM);
const double edgeToEdge =
std::hypot(m_edgeSeparationM, m_y[first] - m_y[second]);
const double rawSource = std::max(
1.0,
((m_y[first] - m_sourceYM) * (m_y[first] - m_y[second]) +
sourceToFirst * edgeToEdge) /
(sourceRadius * m_edgeSeparationM));
const double sourceCosh = std::cosh(ChamferNu * std::acosh(rawSource));
const double sourceDenominator = sourceCosh - sourceAngleCos;
if (!std::isfinite(sourceDenominator) ||
std::abs(sourceDenominator) <= std::numeric_limits<double>::epsilon()) {
return false;
}
const double sourceDirectivity = sourceAngleSin / sourceDenominator;
m_sourceGeometry[index] =
sourceDirectivity / (sourceToFirst * edgeToEdge);
m_sourcePath[index] = sourceToFirst + edgeToEdge;
m_edgePath[index] = edgeToEdge;
if (!prepareReceiverPair(index, edgeToEdge, true) ||
!prepareReceiverPair(index, edgeToEdge, false)) {
return false;
}
}
}
const double intermediateAngle = ChamferNu * Pi;
const double intermediateSin = std::sin(intermediateAngle);
const double intermediateCos = std::cos(intermediateAngle);
for (std::size_t previous = 0; previous < m_count; ++previous) {
for (std::size_t current = 0; current < m_count; ++current) {
const double incoming =
std::hypot(m_edgeSeparationM, m_y[current] - m_y[previous]);
for (std::size_t next = 0; next < m_count; ++next) {
const double outgoing =
std::hypot(m_edgeSeparationM, m_y[current] - m_y[next]);
const double raw = std::max(
1.0,
((m_y[current] - m_y[previous]) *
(m_y[current] - m_y[next]) +
incoming * outgoing) /
(m_edgeSeparationM * m_edgeSeparationM));
const double coshTerm = std::cosh(ChamferNu * std::acosh(raw));
const double denominator = coshTerm - intermediateCos;
if (!std::isfinite(denominator) ||
std::abs(denominator) <= std::numeric_limits<double>::epsilon()) {
return false;
}
const double directivity = intermediateSin / denominator;
m_intermediateGeometry[
tripleIndex(previous, current, next, m_count)] =
directivity / outgoing;
}
}
}
return true;
}
bool evaluate(std::array<std::complex<double>, KFilterFrequencyCount>& higherOrders) const
{
if (m_sourceGeometry.empty() || m_intermediateGeometry.empty()) {
return false;
}
const std::size_t pairCount = m_count * m_count;
std::vector<std::complex<double>> order2State(pairCount);
std::vector<std::complex<double>> order3State(pairCount);
const KFilterFrequencyGrid& frequencies = kfilterFrequencyGridHz();
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double waveNumber = TwoPi * frequencies[sampleIndex] / SpeedOfSoundMPerS;
for (std::size_t first = 0; first < m_count; ++first) {
for (std::size_t second = 0; second < m_count; ++second) {
const std::size_t index = pairIndex(first, second, m_count);
const double phase = -waveNumber * m_sourcePath[index];
order2State[index] =
m_sourceGeometry[index] *
std::complex<double>{std::cos(phase), std::sin(phase)};
}
}
std::complex<double> h2{0.0, 0.0};
for (std::size_t index = 0; index < pairCount; ++index) {
const double phase = -waveNumber * m_receiver2Path[index];
h2 += order2State[index] * m_receiver2Geometry[index] *
std::complex<double>{std::cos(phase), std::sin(phase)};
}
std::fill(order3State.begin(), order3State.end(), std::complex<double>{0.0, 0.0});
for (std::size_t current = 0; current < m_count; ++current) {
for (std::size_t next = 0; next < m_count; ++next) {
std::complex<double> contraction{0.0, 0.0};
for (std::size_t previous = 0; previous < m_count; ++previous) {
contraction +=
order2State[pairIndex(previous, current, m_count)] *
m_intermediateGeometry[
tripleIndex(previous, current, next, m_count)];
}
const double edgePhase =
-waveNumber * m_edgePath[pairIndex(current, next, m_count)];
order3State[pairIndex(current, next, m_count)] =
contraction *
std::complex<double>{std::cos(edgePhase), std::sin(edgePhase)};
}
}
std::complex<double> h3{0.0, 0.0};
for (std::size_t index = 0; index < pairCount; ++index) {
const double phase = -waveNumber * m_receiver1Path[index];
h3 += order3State[index] * m_receiver1Geometry[index] *
std::complex<double>{std::cos(phase), std::sin(phase)};
}
const std::complex<double> value = m_c2 * h2 + m_c3 * h3;
if (!finiteComplex(value)) {
return false;
}
higherOrders[sampleIndex] = value;
}
return true;
}
private:
bool prepareReceiverPair(std::size_t index,
double edgeToEdge,
bool lastEdgeIsSecond)
{
// Analytic receiver-side far-field limit. For two parallel chamfer
// edges, eta depends only on the inter-edge ray, while the on-axis
// receiver angles tend to pi/4 at E2 and pi/2 at E1. The rear E2
// edge retains the additional setback path; E1 has zero receiver-side
// excess path in the chosen front-baffle reference plane.
const double raw = std::max(1.0, edgeToEdge / m_edgeSeparationM);
const double coshTerm = std::cosh(ChamferNu * std::acosh(raw));
const double previousTheta = lastEdgeIsSecond ? 0.0 : ChamferOpenAngleRad;
const double receiverTheta = lastEdgeIsSecond ? Pi / 4.0 : Pi / 2.0;
const double anglePlus = ChamferNu * (Pi + previousTheta + receiverTheta);
const double angleMinus = ChamferNu * (Pi + previousTheta - receiverTheta);
const double denominatorPlus = coshTerm - std::cos(anglePlus);
const double denominatorMinus = coshTerm - std::cos(angleMinus);
if (!std::isfinite(denominatorPlus) || !std::isfinite(denominatorMinus) ||
std::abs(denominatorPlus) <= std::numeric_limits<double>::epsilon() ||
std::abs(denominatorMinus) <= std::numeric_limits<double>::epsilon()) {
return false;
}
const double receiverDirectivity =
std::sin(anglePlus) / denominatorPlus +
std::sin(angleMinus) / denominatorMinus;
if (!std::isfinite(receiverDirectivity)) {
return false;
}
if (lastEdgeIsSecond) {
m_receiver2Geometry[index] = receiverDirectivity;
m_receiver2Path[index] = m_setbackM;
} else {
m_receiver1Geometry[index] = receiverDirectivity;
m_receiver1Path[index] = 0.0;
}
return true;
}
std::size_t m_count = 0;
double m_setbackM = 0.0;
double m_distanceFromOriginalEdgeM = 0.0;
double m_sourceYM = 0.0;
double m_edgeHeightM = 0.0;
double m_edgeSeparationM = 0.0;
double m_dy = 0.0;
double m_c2 = 0.0;
double m_c3 = 0.0;
std::vector<double> m_y;
std::vector<double> m_sourceGeometry;
std::vector<double> m_sourcePath;
std::vector<double> m_intermediateGeometry;
std::vector<double> m_edgePath;
std::vector<double> m_receiver2Geometry;
std::vector<double> m_receiver2Path;
std::vector<double> m_receiver1Geometry;
std::vector<double> m_receiver1Path;
};
}
BaffleChamfer45SideCorrection calculateBaffleChamfer45SideCorrection(
double distanceFromOriginalEdgeM,
double sourceYM,
double edgeHeightM,
double setbackM)
{
BaffleChamfer45SideCorrection correction;
if (!std::isfinite(distanceFromOriginalEdgeM) ||
!std::isfinite(sourceYM) ||
!std::isfinite(edgeHeightM) ||
!std::isfinite(setbackM) ||
distanceFromOriginalEdgeM <= setbackM ||
sourceYM <= 0.0 || sourceYM >= edgeHeightM ||
edgeHeightM <= 0.0 || setbackM <= 0.0) {
return correction;
}
std::array<std::complex<double>, KFilterFrequencyCount> h1Chamfer{};
std::array<std::complex<double>, KFilterFrequencyCount> h1Sharp{};
std::array<std::complex<double>, KFilterFrequencyCount> higherOrders{};
if (!calculateFirstOrderWedge(h1Chamfer,
distanceFromOriginalEdgeM - setbackM,
sourceYM,
edgeHeightM,
3.0 * Pi / 4.0) ||
!calculateFirstOrderWedge(h1Sharp,
distanceFromOriginalEdgeM,
sourceYM,
edgeHeightM,
Pi / 2.0)) {
return correction;
}
HigherOrderChamferKernel higherOrderKernel(
distanceFromOriginalEdgeM, sourceYM, edgeHeightM, setbackM);
if (!higherOrderKernel.prepare() || !higherOrderKernel.evaluate(higherOrders)) {
return correction;
}
for (std::size_t sampleIndex = 0; sampleIndex < KFilterFrequencyCount; ++sampleIndex) {
const double denominatorMagnitude = std::abs(h1Sharp[sampleIndex]);
if (!std::isfinite(denominatorMagnitude) || denominatorMagnitude <= 1.0e-15) {
return BaffleChamfer45SideCorrection{};
}
const std::complex<double> value =
(h1Chamfer[sampleIndex] + higherOrders[sampleIndex]) / h1Sharp[sampleIndex];
if (!finiteComplex(value)) {
return BaffleChamfer45SideCorrection{};
}
correction.values[sampleIndex] = value;
}
correction.valid = true;
return correction;
}