summaryrefslogtreecommitdiff
path: root/src/window-function.cpp
blob: e450cf3822f77b3b087e9ac73a818225c3c192ec (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
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
/*
 * Copyright (c) 2020-2023 Vasile Vilvoiu <vasi@vilvoiu.ro>
 *
 * specgram is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */
#include "window-function.hpp"

#include <cassert>

WindowFunction::WindowFunction(std::size_t window_size) : window_size_(window_size)
{
    cached_factors_.resize(window_size);
}

std::unique_ptr<WindowFunction>
WindowFunction::Build(WindowFunctionType type, std::size_t window_size)
{
    switch (type) {
        case WindowFunctionType::kNone:
            return nullptr; /* no window needed */

        case WindowFunctionType::kHann:
            return std::make_unique<HannWindowFunction>(window_size);

        case WindowFunctionType::kHamming:
            return std::make_unique<HammingWindowFunction>(window_size);

        case WindowFunctionType::kBlackman:
            return std::make_unique<BlackmanWindowFunction>(window_size);

        case WindowFunctionType::kNuttall:
            return std::make_unique<NuttallWindowFunction>(window_size);

        default:
            throw std::runtime_error("unknown window function");
    }
}

ComplexWindow
WindowFunction::Apply(const ComplexWindow& window) const
{
    /* only matching windows */
    if (window.size() != this->window_size_) {
        throw std::runtime_error("incorrect window size for window function application");
    }
    assert(this->cached_factors_.size() == this->window_size_);

    ComplexWindow output;
    output.resize(this->window_size_);
    for (std::size_t i = 0; i < this->window_size_; i++) {
        output[i] = window[i] * this->cached_factors_[i];
    }
    return output;
}

GeneralizedCosineWindowFunction::GeneralizedCosineWindowFunction(std::size_t window_size,
                                                                 const std::vector<double>& a)
    : WindowFunction(window_size)
{
    if (this->window_size_ == 1) {
        /* noop case */
        this->cached_factors_[0] = 1.0;
        return;
    }

    double N = (double)this->window_size_ - 1.0;
    for (std::size_t n = 0; n < this->window_size_; n++) {
        this->cached_factors_[n] = 0;
        for (unsigned int k = 0; k < a.size(); k++) {
            this->cached_factors_[n] +=
                std::pow<double>(-1.0f, k) * a[k] * std::cos(2.0 * (double)M_PI * k * (double)n / N);
        }
    }
}

HannWindowFunction::HannWindowFunction(std::size_t window_size)
    : GeneralizedCosineWindowFunction(window_size, { 0.5, 0.5 })
{
}

HammingWindowFunction::HammingWindowFunction(std::size_t window_size)
        : GeneralizedCosineWindowFunction(window_size, { 0.54, 0.46 })
{
}

BlackmanWindowFunction::BlackmanWindowFunction(std::size_t window_size)
        : GeneralizedCosineWindowFunction(window_size, { 0.42, 0.5, 0.08 })
{
}

NuttallWindowFunction::NuttallWindowFunction(std::size_t window_size)
        : GeneralizedCosineWindowFunction(window_size, { 0.3635819, 0.4891775, 0.1365995, 0.0106411 })
{
}