summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibDSP/MDCT.h
blob: 940013498b98cc2d38952954c0ac30593a82bb40 (plain)
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
/*
 * Copyright (c) 2021, Arne Elster <arne@elster.li>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/Math.h>
#include <AK/Span.h>

namespace LibDSP {

template<size_t N>
requires(N % 2 == 0) class MDCT {
public:
    constexpr MDCT()
    {
        for (size_t n = 0; n < N; n++) {
            for (size_t k = 0; k < N / 2; k++) {
                m_phi[n][k] = AK::cos(AK::Pi<double> / (2 * N) * (2 * n + 1 + N / 2.0) * (2 * k + 1));
            }
        }
    }

    void transform(Span<double const> data, Span<double> output)
    {
        assert(N == 2 * data.size());
        assert(N == output.size());
        for (size_t n = 0; n < N; n++) {
            output[n] = 0;
            for (size_t k = 0; k < N / 2; k++) {
                output[n] += data[k] * m_phi[n][k];
            }
        }
    }

private:
    Array<Array<double, N / 2>, N> m_phi;
};

}