-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac_convert_harm_wave.cpp
84 lines (73 loc) · 2.64 KB
/
ac_convert_harm_wave.cpp
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
/* ac_convert_harm_wave.cpp - Definition of the harmonic to wave converter
* Copyright (C) 2021 Jeanette C. <jeanette@juliencoder.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 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ac_convert_harm_wave.hpp"
#include <cmath>
using std::sin;
using std::abs;
namespace JBS {
Ac_convert_harm_wave::Ac_convert_harm_wave(double zero_threshold, unsigned long int output_size):
Ac_convert_base(zero_threshold, output_size)
{}
Ac_convert_harm_wave::Ac_convert_harm_wave(unsigned long int output_size):
Ac_convert_base(output_size)
{}
bool Ac_convert_harm_wave::convert()
{
bool convert_state = false;
// Only perform if the state is OK
if (its_good == true)
{
if (its_input != nullptr)
{
// Allocate output data
its_output = new double[its_size];
double max_value = 0.0; // Maximum amplitude, used for rescaling
// Set output data values to 0
for (unsigned long sample = 0;sample<its_size;sample++)
{
its_output[sample] = 0.0;
}
// Create the waveform
for (unsigned long int sample = 0;sample<its_size;sample++)
{ // for every sample
for (unsigned long int harmonic = 0;harmonic<its_src_size;harmonic++)
{ // every harmonic
if (abs(its_input[harmonic - 1]) > its_zero_threshold)
{ // only if greater than zero threshold
// Write the point of a sine
its_output[sample] += sin((2 * M_PI * sample * (harmonic + 1)) / its_size) * its_input[harmonic];
}
} // for harmonics
if (abs(its_output[sample]) > max_value) // adjust the maximum
{
max_value = abs(its_output[sample]);
}
} // for samples
// Now rescale the wave to max amplitude 1
for (unsigned long int sample = 0;sample<its_size;sample++)
{
its_output[sample] /= max_value;
}
// Taks complete, set its_ready and convert_state to true
its_ready = true;
convert_state = true;
} // if (its_input != nullptr)
} // if (its_good == true)
return convert_state;
}
} // End of namespace JBS