-
Notifications
You must be signed in to change notification settings - Fork 16
/
Matrix.cs
258 lines (241 loc) · 7.28 KB
/
Matrix.cs
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
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2009 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
abstract public class Matrix
{
// how many channels
protected ushort _dimension;
// square matrix [rows][columns]
protected double[,] _matrix;
// parameter (usually degrees, sometimes something else)
protected double _param = double.NaN;
virtual public double Param
{
get { return _param; }
set { _param = value; }
}
public ISample Process(ISample sample)
{
if (sample.NumChannels != _dimension)
{
throw new InvalidOperationException();
}
Sample s = new Sample(_dimension);
// row of matrix
// channel of output
for (ushort j = 0; j < _dimension; j++)
{
// channel of original sample
// column of matrix
for (ushort c = 0; c < _dimension; c++)
{
s[j] += sample[c] * _matrix[j,c];
}
}
return s;
}
}
abstract public class Amb1Matrix : Matrix
{
public Amb1Matrix()
{
_dimension = 4;
_matrix = new double[4,4];
}
}
// Classic rotation matrices for first-order
// http://www.muse.demon.co.uk/fmhrotat.html
public class Amb1RotAboutX : Amb1Matrix
{
public Amb1RotAboutX(double degrees)
: base()
{
Param = degrees;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
_matrix[0, 0] = 1;
_matrix[1, 1] = 1;
_matrix[2, 2] = Math.Cos(r);
_matrix[3, 2] = Math.Sin(r);
_matrix[2, 3] = -Math.Sin(r);
_matrix[3, 3] = Math.Cos(r);
}
}
}
}
public class Amb1RotAboutY : Amb1Matrix
{
public Amb1RotAboutY(double degrees)
: base()
{
Param = degrees;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
_matrix[0, 0] = 1;
_matrix[1, 1] = Math.Cos(r);
_matrix[3, 1] = Math.Sin(r);
_matrix[2, 2] = 1;
_matrix[1, 3] = -Math.Sin(r);
_matrix[3, 3] = Math.Cos(r);
}
}
}
}
public class Amb1RotAboutZ : Amb1Matrix
{
public Amb1RotAboutZ(double degrees)
: base()
{
Param = degrees;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
_matrix[0, 0] = 1;
_matrix[1, 1] = Math.Cos(r);
_matrix[2, 1] = Math.Sin(r);
_matrix[1, 2] = -Math.Sin(r);
_matrix[2, 2] = Math.Cos(r);
_matrix[3, 3] = 1;
}
}
}
}
// Classic dominance and
// Joseph Anderson's push etc
// http://ambisonics.iem.at/symposium2009/proceedings/ambisym09-josephanderson-ambitk-poster.pdf/at_download/file
public class Amb1Dominance : Amb1Matrix
{
private double _az, _el;
public Amb1Dominance(double lambda, double az, double el)
: base()
{
Param = lambda;
_az = az;
_el = el;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double lambda = value;
_matrix[0, 0] = 0.5 * (lambda + (1 / lambda));
_matrix[1, 0] = (1 / Math.Sqrt(2)) * (lambda - (1 / lambda));
_matrix[0, 1] = (1 / Math.Sqrt(8)) * (lambda - (1 / lambda));
_matrix[1, 1] = 0.5 * (lambda + (1 / lambda));
_matrix[2, 2] = 1;
_matrix[3, 3] = 1;
}
}
}
}
public class Amb1Focus : Amb1Matrix
{
private double _az, _el;
public Amb1Focus(double degrees, double az, double el)
: base()
{
Param = degrees;
_az = az;
_el = el;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
double sinr = Math.Sin(r);
double sqr2 = Math.Sqrt(2);
_matrix[0, 0] = (1 / (1 + Math.Abs(sinr)));
_matrix[1, 0] = sqr2 * (sinr / (1 + Math.Abs(sinr)));
_matrix[0, 1] = (1 / sqr2) * (sinr / (1 + Math.Abs(sinr)));
_matrix[1, 1] = (1 / (1 + Math.Abs(sinr)));
_matrix[2, 2] = Math.Sqrt((1 - Math.Abs(sinr)) / (1 + Math.Abs(sinr)));
_matrix[3, 3] = Math.Sqrt((1 - Math.Abs(sinr)) / (1 + Math.Abs(sinr)));
}
}
}
}
public class Amb1Push : Amb1Matrix
{
private double _az, _el;
public Amb1Push(double degrees, double az, double el)
: base()
{
Param = degrees;
_az = az;
_el = el;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
_matrix[0, 0] = 1;
_matrix[1, 0] = Math.Sqrt(2) * Math.Abs(Math.Sin(r)) * Math.Sin(r);
_matrix[1, 1] = Math.Cos(r) * Math.Cos(r);
_matrix[2, 2] = Math.Cos(r) * Math.Cos(r);
_matrix[3, 3] = Math.Cos(r) * Math.Cos(r);
}
}
}
}
public class Amb1Press : Amb1Matrix
{
private double _az, _el;
public Amb1Press(double degrees, double az, double el)
: base()
{
Param = degrees;
_az = az;
_el = el;
}
override public double Param
{
set
{
if (_param != value)
{
_param = value;
double r = MathUtil.Radians(value);
_matrix[0, 0] = 1;
_matrix[1, 0] = Math.Sqrt(2) * Math.Abs(Math.Sin(r)) * Math.Sin(r);
_matrix[1, 1] = Math.Cos(r) * Math.Cos(r);
_matrix[2, 2] = Math.Cos(r);
_matrix[3, 3] = Math.Cos(r);
}
}
}
}
}