OpenShot Audio Library | OpenShotAudio: juce_FIRFilter.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0 |

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