LASP 1.0
Library for Acoustic Signal Processing
Loading...
Searching...
No Matches
lasp_avpowerspectra.cpp
Go to the documentation of this file.
1/* #define DEBUGTRACE_ENABLED */
3#include "debugtrace.hpp"
4#include "lasp_mathtypes.h"
5#include <cmath>
6#include <optional>
7
8using rte = std::runtime_error;
9using std::cerr;
10using std::endl;
11
13 : PowerSpectra(Window::create(w, nfft)) {}
14
16 : nfft(window.size()), _fft(nfft), _window(window) {
17
18 d win_pow = arma::sum(window % window) / window.size();
19
20 /* Scale fft such that power is easily computed */
21 _scale_fac = 2.0 / (win_pow * (d)nfft * (d)nfft);
22
23 DEBUGTRACE_PRINT(nfft);
24 DEBUGTRACE_PRINT(win_pow);
25}
26
28
30 /* DEBUGTRACE_ENTER; */
31
32 dmat input_tmp = input;
33
34 // Multiply each column of the inputs element-wise with the window.
35 input_tmp.each_col() %= _window;
36
37 cmat rfft = _fft.fft(input_tmp);
38
39 ccube output(rfft.n_rows, input.n_cols, input.n_cols);
40 for (us i = 0; i < input.n_cols; i++) {
43#pragma omp parallel for
44 for (us j = 0; j < input.n_cols; j++) {
45 output.slice(j).col(i) =
46 _scale_fac * (rfft.col(i) % arma::conj(rfft.col(j)));
47
48 output.slice(j).col(i)(0) /= 2;
49 output.slice(j).col(i)(nfft / 2) /= 2;
50 }
51 }
52 return output;
53}
54
56 const d overlap_percentage,
57 const d time_constant_times_fs)
58 : _ps(nfft, w) {
59
60 DEBUGTRACE_ENTER;
61 if (overlap_percentage >= 100 || overlap_percentage < 0) {
62 throw rte("Overlap percentage should be >= 0 and < 100");
63 }
64
65 _overlap_keep = (nfft * overlap_percentage) / 100;
66 DEBUGTRACE_PRINT(_overlap_keep);
67 if (_overlap_keep >= nfft) {
68 throw rte("Overlap is too high. Results in no jump. Please "
69 "choose a smaller overlap percentage or a higher nfft");
70 }
71 if (time_constant_times_fs < 0) {
72 _mode = Mode::Averaging;
73 } else if (time_constant_times_fs == 0) {
74 _mode = Mode::Spectrogram;
75 } else {
76 _mode = Mode::Leaking;
77 _alpha = d_exp(-static_cast<d>((nfft - _overlap_keep)/time_constant_times_fs));
78 DEBUGTRACE_PRINT(_alpha);
79 }
80}
82 _timeBuf.reset();
83 _est.reset();
84 n_averages=0;
85
86}
87
89
90 DEBUGTRACE_ENTER;
91 DEBUGTRACE_PRINT(timedata.n_rows);
92 DEBUGTRACE_PRINT(_ps.nfft);
93
94 _timeBuf.push(timedata);
95
96 bool run_once = false;
97 while (_timeBuf.size() >= _ps.nfft) {
98
99 DEBUGTRACE_PRINT((int)_mode);
100
101 dmat samples = _timeBuf.pop(_ps.nfft, _overlap_keep);
102
103 switch (_mode) {
104 case (Mode::Spectrogram):
105 DEBUGTRACE_PRINT("Spectrogram mode");
106 _est = _ps.compute(samples);
107 break;
108 case (Mode::Averaging):
109 DEBUGTRACE_PRINT("Averaging mode");
110 n_averages++;
111 if (n_averages == 1) {
112 _est = _ps.compute(samples);
113 } else {
114 _est = (static_cast<d>(n_averages - 1) / n_averages) * _est +
115 _ps.compute(samples) / n_averages;
116 }
117 break;
118 case (Mode::Leaking):
119 DEBUGTRACE_PRINT("Leaking mode");
120 if (arma::size(_est) == arma::size(0, 0, 0)) {
121 _est = _ps.compute(samples);
122 } else {
123 _est = _alpha * _est + (1 - _alpha) * _ps.compute(samples);
124 }
125 break;
126 } // end switch mode
127 run_once = true;
128 }
129
132 return run_once ? _est : ccube();
133}
135 return _est;
136}
ccube get_est() const
Returns the latest estimate of cps (cross-power spectra.
ccube compute(const dmat &timedata)
Compute an update of the power spectra based on given time data. Note that the number of channels is ...
AvPowerSpectra(const us nfft=2048, const Window::WindowType w=Window::WindowType::Hann, const d overlap_percentage=50., const d fs_tau=-1)
Initalize averaged power spectra computer. If a time constant is given > 0, it is used in a kind of e...
void reset()
Reset to empty state. Clears the time buffer and sets estimator to empty.
vc fft(const vd &timedata)
Definition lasp_fft.cpp:126
Computes single-sided cross-power spectra for a group of channels. Only a single block of length fft,...
ccube compute(const dmat &input)
Computes the spectra. Data is normalized by the (spectral) power of the window, to compensate for the...
PowerSpectra(const us nfft, const Window::WindowType w)
Initalize power spectra computer.
us nfft
The FFT length.
dmat pop(const us nframes, const us keep=0)
Pop frames from the buffer. Throws a runtime error if more frames are requested than actually stored.
us size() const
Returns current size of stored amount of frames.
void reset()
Reset (empties) the time buffer.
void push(const dmat &mat)
Put samples in the buffer. Number of channels should match other frames, otherwise things go wrong.
Window (aka taper) functions of a certain type.
Definition lasp_window.h:7
std::runtime_error rte
Definition lasp_daq.cpp:16
arma::Col< d > vd
arma::Mat< d > dmat
#define d_exp
arma::Cube< c > ccube
arma::Mat< c > cmat
size_t us
We often use boolean values.
Definition lasp_types.h:29