OpenShot Audio Library | OpenShotAudio: juce_FIRFilter.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
juce_FIRFilter.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11 Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12 27th April 2017).
13
14 End User License Agreement: www.juce.com/juce-5-licence
15 Privacy Policy: www.juce.com/juce-5-privacy-policy
16
17 Or: You may also use this code under the terms of the GPL v3 (see
18 www.gnu.org/licenses).
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29namespace dsp
30{
31
32template <typename NumericType>
33double FIR::Coefficients<NumericType>::Coefficients::getMagnitudeForFrequency (double frequency, double theSampleRate) const noexcept
34{
35 jassert (theSampleRate > 0.0);
36 jassert (frequency >= 0.0 && frequency <= theSampleRate * 0.5);
37
38 constexpr Complex<double> j (0, 1);
39 auto order = getFilterOrder();
40
41 Complex<double> numerator = 0.0, factor = 1.0;
42 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequency * j / theSampleRate);
43
44 const auto* coefs = coefficients.begin();
45
46 for (size_t n = 0; n <= order; ++n)
47 {
48 numerator += static_cast<double> (coefs[n]) * factor;
49 factor *= jw;
50 }
51
52 return std::abs (numerator);
53}
54
55//==============================================================================
56template <typename NumericType>
57void FIR::Coefficients<NumericType>::Coefficients::getMagnitudeForFrequencyArray (double* frequencies, double* magnitudes,
58 size_t numSamples, double theSampleRate) const noexcept
59{
60 jassert (theSampleRate > 0.0);
61
62 constexpr Complex<double> j (0, 1);
63 const auto* coefs = coefficients.begin();
64 auto order = getFilterOrder();
65
66 for (size_t i = 0; i < numSamples; ++i)
67 {
68 jassert (frequencies[i] >= 0.0 && frequencies[i] <= theSampleRate * 0.5);
69
70 Complex<double> numerator = 0.0;
71 Complex<double> factor = 1.0;
72 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequencies[i] * j / theSampleRate);
73
74 for (size_t n = 0; n <= order; ++n)
75 {
76 numerator += static_cast<double> (coefs[n]) * factor;
77 factor *= jw;
78 }
79
80 magnitudes[i] = std::abs (numerator);
81 }
82}
83
84//==============================================================================
85template <typename NumericType>
86double FIR::Coefficients<NumericType>::Coefficients::getPhaseForFrequency (double frequency, double theSampleRate) const noexcept
87{
88 jassert (theSampleRate > 0.0);
89 jassert (frequency >= 0.0 && frequency <= theSampleRate * 0.5);
90
91 constexpr Complex<double> j (0, 1);
92
93 Complex<double> numerator = 0.0;
94 Complex<double> factor = 1.0;
95 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequency * j / theSampleRate);
96
97 const auto* coefs = coefficients.begin();
98 auto order = getFilterOrder();
99
100 for (size_t n = 0; n <= order; ++n)
101 {
102 numerator += static_cast<double> (coefs[n]) * factor;
103 factor *= jw;
104 }
105
106 return std::arg (numerator);
107}
108
109//==============================================================================
110template <typename NumericType>
111void FIR::Coefficients<NumericType>::Coefficients::getPhaseForFrequencyArray (double* frequencies, double* phases,
112 size_t numSamples, double theSampleRate) const noexcept
113{
114 jassert (theSampleRate > 0.0);
115
116 constexpr Complex<double> j (0, 1);
117 const auto* coefs = coefficients.begin();
118 auto order = getFilterOrder();
119
120 for (size_t i = 0; i < numSamples; ++i)
121 {
122 jassert (frequencies[i] >= 0.0 && frequencies[i] <= theSampleRate * 0.5);
123
124 Complex<double> numerator = 0.0, factor = 1.0;
125 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequencies[i] * j / theSampleRate);
126
127 for (size_t n = 0; n <= order; ++n)
128 {
129 numerator += static_cast<double> (coefs[n]) * factor;
130 factor *= jw;
131 }
132
133 phases[i] = std::arg (numerator);
134 }
135}
136
137//==============================================================================
138template <typename NumericType>
139void FIR::Coefficients<NumericType>::Coefficients::normalise() noexcept
140{
141 auto magnitude = static_cast<NumericType> (0);
142
143 auto* coefs = coefficients.getRawDataPointer();
144 auto n = static_cast<size_t> (coefficients.size());
145
146 for (size_t i = 0; i < n; ++i)
147 {
148 auto c = coefs[i];
149 magnitude += c * c;
150 }
151
152 auto magnitudeInv = 1 / (4 * std::sqrt (magnitude));
153
154 FloatVectorOperations::multiply (coefs, magnitudeInv, static_cast<int> (n));
155}
156
157//==============================================================================
158template struct FIR::Coefficients<float>;
159template struct FIR::Coefficients<double>;
160
161} // namespace dsp
162} // namespace juce
juce::FloatVectorOperations::multiply
static void JUCE_CALLTYPE multiply(float *dest, const float *src, int numValues) noexcept
Definition: juce_FloatVectorOperations.cpp:754
juce::MathConstants::twoPi
static const FloatType twoPi
Definition: juce_MathsFunctions.h:368