File: WindowFunction.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (106 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
/***************************************************************************
     WindowFunction.cpp  -  Windows functions for signal processing
                             -------------------
    begin                : Feb 05 2001
    copyright            : (C) 2001 by Thomas Eschenbacher
    email                : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"
#include <KLocalizedString>
#include <math.h>

#include "libkwave/String.h"
#include "libkwave/Utils.h"
#include "libkwave/WindowFunction.h"

//***************************************************************************
//***************************************************************************

void Kwave::WindowFunction::InitializedTypesMap::fill()
{
    append(WINDOW_FUNC_NONE,       WINDOW_FUNC_NONE,
           _("none"),       kli18n("None"));
    append(WINDOW_FUNC_HAMMING,    WINDOW_FUNC_HAMMING,
           _("hamming"),    kli18n("Hamming"));
    append(WINDOW_FUNC_HANNING,    WINDOW_FUNC_HANNING,
           _("hanning"),    kli18n("Hanning"));
    append(WINDOW_FUNC_BLACKMAN,   WINDOW_FUNC_BLACKMAN,
           _("blackman"),   kli18n("Blackman"));
    append(WINDOW_FUNC_TRIANGULAR, WINDOW_FUNC_TRIANGULAR,
           _("triangular"), kli18n("Triangular"));
}

//***************************************************************************
// static initializer
Kwave::WindowFunction::InitializedTypesMap Kwave::WindowFunction::m_types_map;

//***************************************************************************
//***************************************************************************
Kwave::WindowFunction::WindowFunction(Kwave::window_function_t type)
    :m_type(type)
{
}

//***************************************************************************
Kwave::WindowFunction::~WindowFunction()
{
}

//***************************************************************************
QVector<double> Kwave::WindowFunction::points(unsigned int len) const
{
    QVector<double> out(len);
    Q_ASSERT(out.count() == Kwave::toInt(len));
    if (out.count() != Kwave::toInt(len)) {
        out.resize(0);
        return out;
    }

    // Hanning, Hamming, Blackman window functions as proposed
    // in Oppenheim, Schafer, p.241 ff
    switch (m_type) {
        case WINDOW_FUNC_NONE:   //rectangular window
            for (unsigned int i = 0; i < len; i++)
                out[i] = 1;
            break;
        case WINDOW_FUNC_HANNING:
            for (unsigned int i = 0; i < len; i++)
                out[i] = 0.5 * (1 - cos(i * 2 * M_PI / (len - 1)));
            break;
        case WINDOW_FUNC_HAMMING:
            for (unsigned int i = 0; i < len; i++)
                out[i] = 0.54-(0.46 * cos(static_cast<double>(i) * 2 * M_PI /
                         (len - 1)));
            break;
        case WINDOW_FUNC_BLACKMAN:
            for (unsigned int i = 0; i < len; i++)
                out[i] = 0.42-(0.50 * cos(static_cast<double>(i) * 2 * M_PI /
                         (len - 1))) +
                         (0.08 * cos(static_cast<double>(i) * 4 * M_PI /
                         (len - 1)));
            break;
        case WINDOW_FUNC_TRIANGULAR:
            for (unsigned int i = 0; i < len / 2; i++)
                out[i] = static_cast<double>(i) / (len / 2 - 1);

            for (unsigned int i = len / 2; i < len; i++)
                out[i] = 1 - (static_cast<double>(i) - len / 2) / (len / 2 - 1);
            break;
        DEFAULT_IMPOSSIBLE;
    }

    return out;
}

//***************************************************************************
//***************************************************************************