]> git.sesse.net Git - nageru/blob - correlation_measurer.h
Release Nageru 1.7.2.
[nageru] / correlation_measurer.h
1 #ifndef _CORRELATION_MEASURER_H
2 #define _CORRELATION_MEASURER_H 1
3
4 // Measurement of left/right stereo correlation. +1 is pure mono
5 // (okay but not ideal), 0 is no correlation (usually bad, unless
6 // it is due to silence), strongly negative values means inverted
7 // phase (bad). Typical values for e.g. music would be somewhere
8 // around +0.7, although you can expect it to vary a bit.
9 //
10 // This is, of course, based on the regular Pearson correlation,
11 // where µ_L and µ_R is taken to be 0 (ie., no DC offset). It is
12 // filtered through a simple IIR filter so that older values are
13 // weighed less than newer, depending on <falloff_seconds>.
14 //
15 //
16 // Adapted from Adriaensen's project Zita-mu1 (as of January 2016).
17 // Original copyright follows:
18 //
19 //  Copyright (C) 2008-2015 Fons Adriaensen <fons@linuxaudio.org>
20 //    
21 //  This program is free software; you can redistribute it and/or modify
22 //  it under the terms of the GNU General Public License as published by
23 //  the Free Software Foundation; either version 3 of the License, or
24 //  (at your option) any later version.
25 //
26 //  This program is distributed in the hope that it will be useful,
27 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
28 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 //  GNU General Public License for more details.
30 //
31 //  You should have received a copy of the GNU General Public License
32 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
33
34 #include <vector>
35
36 class CorrelationMeasurer {
37 public:
38         CorrelationMeasurer(unsigned sample_rate, float lowpass_cutoff_hz = 1000.0f,
39                             float falloff_seconds = 0.150f);
40         void process_samples(const std::vector<float> &samples);  // Taken to be stereo, interleaved.
41         void reset();
42         float get_correlation() const;
43
44 private:
45         float w1, w2;
46
47         // Filtered values of left and right channel, respectively.
48         float zl = 0.0f, zr = 0.0f;
49
50         // Filtered values of l², r² and lr (where l and r are the filtered
51         // versions, given by zl and zr). Together, they make up what we need
52         // to calculate the correlation.
53         float zll = 0.0f, zlr = 0.0f, zrr = 0.0f;
54 };
55
56 #endif  // !defined(_CORRELATION_MEASURER_H)