Audacity 3.2.0
Classes | Functions
Matrix.h File Reference

General routine to interpolate (or even extrapolate small amounts) audio when a few of the samples are bad. Works great for a few dozen bad samples, but not so well with hundreds. Uses the least-squares autoregression (LSAR) algorithm, as described in: More...

#include "SampleFormat.h"
Include dependency graph for Matrix.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Vector
 Holds a matrix of doubles and supports arithmetic operations, including Vector-Matrix operations. Used by InterpolateAudio. More...
 
class  Matrix
 Holds a matrix of doubles and supports arithmetic, subsetting, and matrix inversion. Used by InterpolateAudio. More...
 

Functions

bool InvertMatrix (const Matrix &input, Matrix &Minv)
 
Matrix TransposeMatrix (const Matrix &M)
 
Matrix IdentityMatrix (unsigned N)
 
Vector operator+ (const Vector &left, const Vector &right)
 
Vector operator- (const Vector &left, const Vector &right)
 
Vector operator* (const Vector &left, const Vector &right)
 
Vector operator* (const Vector &left, double right)
 
Vector VectorSubset (const Vector &other, unsigned start, unsigned len)
 
Vector VectorConcatenate (const Vector &left, const Vector &right)
 
Vector operator* (const Vector &left, const Matrix &right)
 
Vector operator* (const Matrix &left, const Vector &right)
 
Matrix operator+ (const Matrix &left, const Matrix &right)
 
Matrix operator* (const Matrix &left, const double right)
 
Matrix ScalarMultiply (const Matrix &left, const Matrix &right)
 
Matrix MatrixMultiply (const Matrix &left, const Matrix &right)
 
Matrix MatrixSubset (const Matrix &M, unsigned startRow, unsigned numRows, unsigned startCol, unsigned numCols)
 
Matrix MatrixConcatenateCols (const Matrix &left, const Matrix &right)
 

Detailed Description

General routine to interpolate (or even extrapolate small amounts) audio when a few of the samples are bad. Works great for a few dozen bad samples, but not so well with hundreds. Uses the least-squares autoregression (LSAR) algorithm, as described in:

Holds both the Matrix and Vector classes, supporting linear algebra operations, including matrix inversion. Used by InterpolateAudio.

Simon Godsill, Peter Rayner, and Olivier Cappe. Digital Audio Restoration. Berlin: Springer, 1998.

This is the same work used by Gnome Wave Cleaner (GWC), however this implementation is original.

Definition in file Matrix.h.

Function Documentation

◆ IdentityMatrix()

Matrix IdentityMatrix ( unsigned  N)

Definition at line 127 of file Matrix.cpp.

128{
129 Matrix M(N, N);
130 for(unsigned i = 0; i < N; i++)
131 M[i][i] = 1.0;
132 return M;
133}
Holds a matrix of doubles and supports arithmetic, subsetting, and matrix inversion....
Definition: Matrix.h:58

Referenced by InvertMatrix().

Here is the caller graph for this function:

◆ InvertMatrix()

bool InvertMatrix ( const Matrix input,
Matrix Minv 
)

Definition at line 289 of file Matrix.cpp.

290{
291 // Very straightforward implementation of
292 // Gauss-Jordan elimination to invert a matrix.
293 // Returns true if successful
294
295 wxASSERT(input.Rows() == input.Cols());
296 auto N = input.Rows();
297
298 Matrix M = input;
299 Minv = IdentityMatrix(N);
300
301 // Do the elimination one column at a time
302 for(unsigned i = 0; i < N; i++) {
303 // Pivot the row with the largest absolute value in
304 // column i, into row i
305 double absmax = 0.0;
306 unsigned int argmax = 0;
307
308 for(unsigned j = i; j < N; j++)
309 if (fabs(M[j][i]) > absmax) {
310 absmax = fabs(M[j][i]);
311 argmax = j;
312 }
313
314 // If no row has a nonzero value in that column,
315 // the matrix is singular and we have to give up.
316 if (absmax == 0)
317 return false;
318
319 if (i != argmax) {
320 M.SwapRows(i, argmax);
321 Minv.SwapRows(i, argmax);
322 }
323
324 // Divide this row by the value of M[i][i]
325 double factor = 1.0 / M[i][i];
326 M[i] = M[i] * factor;
327 Minv[i] = Minv[i] * factor;
328
329 // Eliminate the rest of the column
330 for(unsigned j = 0; j < N; j++) {
331 if (j == i)
332 continue;
333 if (fabs(M[j][i]) > 0) {
334 // Subtract a multiple of row i from row j
335 factor = M[j][i];
336 for(unsigned k = 0; k < N; k++) {
337 M[j][k] -= (M[i][k] * factor);
338 Minv[j][k] -= (Minv[i][k] * factor);
339 }
340 }
341 }
342 }
343
344 return true;
345}
Matrix IdentityMatrix(unsigned N)
Definition: Matrix.cpp:127
unsigned Cols() const
Definition: Matrix.h:69
unsigned Rows() const
Definition: Matrix.h:68
void SwapRows(unsigned i, unsigned j)
Definition: Matrix.cpp:122

References Matrix::Cols(), IdentityMatrix(), Matrix::Rows(), and Matrix::SwapRows().

Referenced by InterpolateAudio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MatrixConcatenateCols()

Matrix MatrixConcatenateCols ( const Matrix left,
const Matrix right 
)

Definition at line 267 of file Matrix.cpp.

268{
269 wxASSERT(left.Rows() == right.Rows());
270 Matrix M(left.Rows(), left.Cols() + right.Cols());
271 for(unsigned i = 0; i < left.Rows(); i++) {
272 for(unsigned j = 0; j < left.Cols(); j++)
273 M[i][j] = left[i][j];
274 for(unsigned j = 0; j < right.Cols(); j++)
275 M[i][j+left.Cols()] = right[i][j];
276 }
277 return M;
278}

References Matrix::Cols(), and Matrix::Rows().

Referenced by InterpolateAudio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MatrixMultiply()

Matrix MatrixMultiply ( const Matrix left,
const Matrix right 
)

Definition at line 243 of file Matrix.cpp.

244{
245 wxASSERT(left.Cols() == right.Rows());
246 Matrix M(left.Rows(), right.Cols());
247 for(unsigned i = 0; i < left.Rows(); i++)
248 for(unsigned j = 0; j < right.Cols(); j++) {
249 M[i][j] = 0.0;
250 for(unsigned k = 0; k < left.Cols(); k++)
251 M[i][j] += left[i][k] * right[k][j];
252 }
253 return M;
254}

References Matrix::Cols(), and Matrix::Rows().

Referenced by InterpolateAudio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MatrixSubset()

Matrix MatrixSubset ( const Matrix M,
unsigned  startRow,
unsigned  numRows,
unsigned  startCol,
unsigned  numCols 
)

Definition at line 256 of file Matrix.cpp.

259{
260 Matrix M(numRows, numCols);
261 for(unsigned i = 0; i < numRows; i++)
262 for(unsigned j = 0; j < numCols; j++)
263 M[i][j] = input[startRow+i][startCol+j];
264 return M;
265}

Referenced by InterpolateAudio().

Here is the caller graph for this function:

◆ operator*() [1/5]

Matrix operator* ( const Matrix left,
const double  right 
)

Definition at line 223 of file Matrix.cpp.

224{
225 Matrix M(left.Rows(), left.Cols());
226 for(unsigned i = 0; i < left.Rows(); i++)
227 for(unsigned j = 0; j < left.Cols(); j++)
228 M[i][j] = left[i][j] * right;
229 return M;
230}

References Matrix::Cols(), and Matrix::Rows().

Here is the call graph for this function:

◆ operator*() [2/5]

Vector operator* ( const Matrix left,
const Vector right 
)

Definition at line 200 of file Matrix.cpp.

201{
202 wxASSERT(left.Cols() == right.Len());
203 Vector v(left.Rows());
204 for(unsigned i = 0; i < left.Rows(); i++) {
205 v[i] = 0.0;
206 for(unsigned j = 0; j < left.Cols(); j++)
207 v[i] += left[i][j] * right[j];
208 }
209 return v;
210}
Holds a matrix of doubles and supports arithmetic operations, including Vector-Matrix operations....
Definition: Matrix.h:34
unsigned Len() const
Definition: Matrix.h:48

References Matrix::Cols(), Vector::Len(), and Matrix::Rows().

Here is the call graph for this function:

◆ operator*() [3/5]

Vector operator* ( const Vector left,
const Matrix right 
)

Definition at line 188 of file Matrix.cpp.

189{
190 wxASSERT(left.Len() == right.Rows());
191 Vector v(right.Cols());
192 for(unsigned i = 0; i < right.Cols(); i++) {
193 v[i] = 0.0;
194 for(unsigned j = 0; j < right.Rows(); j++)
195 v[i] += left[j] * right[j][i];
196 }
197 return v;
198}

References Matrix::Cols(), Vector::Len(), and Matrix::Rows().

Here is the call graph for this function:

◆ operator*() [4/5]

Vector operator* ( const Vector left,
const Vector right 
)

Definition at line 153 of file Matrix.cpp.

154{
155 wxASSERT(left.Len() == right.Len());
156 Vector v(left.Len());
157 for(unsigned i = 0; i < left.Len(); i++)
158 v[i] = left[i] * right[i];
159 return v;
160}

References Vector::Len().

Here is the call graph for this function:

◆ operator*() [5/5]

Vector operator* ( const Vector left,
double  right 
)

Definition at line 162 of file Matrix.cpp.

163{
164 Vector v(left.Len());
165 for(unsigned i = 0; i < left.Len(); i++)
166 v[i] = left[i] * right;
167 return v;
168}

References Vector::Len().

Here is the call graph for this function:

◆ operator+() [1/2]

Matrix operator+ ( const Matrix left,
const Matrix right 
)

Definition at line 212 of file Matrix.cpp.

213{
214 wxASSERT(left.Rows() == right.Rows());
215 wxASSERT(left.Cols() == right.Cols());
216 Matrix M(left.Rows(), left.Cols());
217 for(unsigned i = 0; i < left.Rows(); i++)
218 for(unsigned j = 0; j < left.Cols(); j++)
219 M[i][j] = left[i][j] + right[i][j];
220 return M;
221}

References Matrix::Cols(), and Matrix::Rows().

Here is the call graph for this function:

◆ operator+() [2/2]

Vector operator+ ( const Vector left,
const Vector right 
)

Definition at line 135 of file Matrix.cpp.

136{
137 wxASSERT(left.Len() == right.Len());
138 Vector v(left.Len());
139 for(unsigned i = 0; i < left.Len(); i++)
140 v[i] = left[i] + right[i];
141 return v;
142}

References Vector::Len().

Here is the call graph for this function:

◆ operator-()

Vector operator- ( const Vector left,
const Vector right 
)

Definition at line 144 of file Matrix.cpp.

145{
146 wxASSERT(left.Len() == right.Len());
147 Vector v(left.Len());
148 for(unsigned i = 0; i < left.Len(); i++)
149 v[i] = left[i] - right[i];
150 return v;
151}

References Vector::Len().

Here is the call graph for this function:

◆ ScalarMultiply()

Matrix ScalarMultiply ( const Matrix left,
const Matrix right 
)

Definition at line 232 of file Matrix.cpp.

233{
234 wxASSERT(left.Rows() == right.Rows());
235 wxASSERT(left.Cols() == right.Cols());
236 Matrix M(left.Rows(), left.Cols());
237 for(unsigned i = 0; i < left.Rows(); i++)
238 for(unsigned j = 0; j < left.Cols(); j++)
239 M[i][j] = left[i][j] * right[i][j];
240 return M;
241}

References Matrix::Cols(), and Matrix::Rows().

Here is the call graph for this function:

◆ TransposeMatrix()

Matrix TransposeMatrix ( const Matrix M)

Definition at line 280 of file Matrix.cpp.

281{
282 Matrix M(other.Cols(), other.Rows());
283 for(unsigned i = 0; i < other.Rows(); i++)
284 for(unsigned j = 0; j < other.Cols(); j++)
285 M[j][i] = other[i][j];
286 return M;
287}

References Matrix::Cols(), and Matrix::Rows().

Referenced by InterpolateAudio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VectorConcatenate()

Vector VectorConcatenate ( const Vector left,
const Vector right 
)

Definition at line 178 of file Matrix.cpp.

179{
180 Vector v(left.Len() + right.Len());
181 for(unsigned i = 0; i < left.Len(); i++)
182 v[i] = left[i];
183 for(unsigned i = 0; i < right.Len(); i++)
184 v[i + left.Len()] = right[i];
185 return v;
186}

References Vector::Len().

Referenced by InterpolateAudio().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VectorSubset()

Vector VectorSubset ( const Vector other,
unsigned  start,
unsigned  len 
)

Definition at line 170 of file Matrix.cpp.

171{
172 Vector v(len);
173 for(unsigned i = 0; i < len; i++)
174 v[i] = other[start+i];
175 return v;
176}

Referenced by InterpolateAudio().

Here is the caller graph for this function: