LMMS
Loading...
Searching...
No Matches
juce_FIRFilter.cpp
Go to the documentation of this file.
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
27{
28namespace dsp
29{
30
31template <typename NumericType>
32double FIR::Coefficients<NumericType>::Coefficients::getMagnitudeForFrequency (double frequency, double theSampleRate) const noexcept
33{
34 jassert (theSampleRate > 0.0);
35 jassert (frequency >= 0.0 && frequency <= theSampleRate * 0.5);
36
37 constexpr Complex<double> j (0, 1);
38 auto order = getFilterOrder();
39
40 Complex<double> numerator = 0.0, factor = 1.0;
41 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequency * j / theSampleRate);
42
43 const auto* coefs = coefficients.begin();
44
45 for (size_t n = 0; n <= order; ++n)
46 {
47 numerator += static_cast<double> (coefs[n]) * factor;
48 factor *= jw;
49 }
50
51 return std::abs (numerator);
52}
53
54//==============================================================================
55template <typename NumericType>
56void FIR::Coefficients<NumericType>::Coefficients::getMagnitudeForFrequencyArray (double* frequencies, double* magnitudes,
57 size_t numSamples, double theSampleRate) const noexcept
58{
59 jassert (theSampleRate > 0.0);
60
61 constexpr Complex<double> j (0, 1);
62 const auto* coefs = coefficients.begin();
63 auto order = getFilterOrder();
64
65 for (size_t i = 0; i < numSamples; ++i)
66 {
67 jassert (frequencies[i] >= 0.0 && frequencies[i] <= theSampleRate * 0.5);
68
69 Complex<double> numerator = 0.0;
70 Complex<double> factor = 1.0;
71 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequencies[i] * j / theSampleRate);
72
73 for (size_t n = 0; n <= order; ++n)
74 {
75 numerator += static_cast<double> (coefs[n]) * factor;
76 factor *= jw;
77 }
78
79 magnitudes[i] = std::abs (numerator);
80 }
81}
82
83//==============================================================================
84template <typename NumericType>
85double FIR::Coefficients<NumericType>::Coefficients::getPhaseForFrequency (double frequency, double theSampleRate) const noexcept
86{
87 jassert (theSampleRate > 0.0);
88 jassert (frequency >= 0.0 && frequency <= theSampleRate * 0.5);
89
90 constexpr Complex<double> j (0, 1);
91
92 Complex<double> numerator = 0.0;
93 Complex<double> factor = 1.0;
94 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequency * j / theSampleRate);
95
96 const auto* coefs = coefficients.begin();
97 auto order = getFilterOrder();
98
99 for (size_t n = 0; n <= order; ++n)
100 {
101 numerator += static_cast<double> (coefs[n]) * factor;
102 factor *= jw;
103 }
104
105 return std::arg (numerator);
106}
107
108//==============================================================================
109template <typename NumericType>
110void FIR::Coefficients<NumericType>::Coefficients::getPhaseForFrequencyArray (double* frequencies, double* phases,
111 size_t numSamples, double theSampleRate) const noexcept
112{
113 jassert (theSampleRate > 0.0);
114
115 constexpr Complex<double> j (0, 1);
116 const auto* coefs = coefficients.begin();
117 auto order = getFilterOrder();
118
119 for (size_t i = 0; i < numSamples; ++i)
120 {
121 jassert (frequencies[i] >= 0.0 && frequencies[i] <= theSampleRate * 0.5);
122
123 Complex<double> numerator = 0.0, factor = 1.0;
124 Complex<double> jw = std::exp (-MathConstants<double>::twoPi * frequencies[i] * j / theSampleRate);
125
126 for (size_t n = 0; n <= order; ++n)
127 {
128 numerator += static_cast<double> (coefs[n]) * factor;
129 factor *= jw;
130 }
131
132 phases[i] = std::arg (numerator);
133 }
134}
135
136//==============================================================================
137template <typename NumericType>
138void FIR::Coefficients<NumericType>::Coefficients::normalise() noexcept
139{
140 auto magnitude = static_cast<NumericType> (0);
141
142 auto* coefs = coefficients.getRawDataPointer();
143 auto n = static_cast<size_t> (coefficients.size());
144
145 for (size_t i = 0; i < n; ++i)
146 {
147 auto c = coefs[i];
148 magnitude += c * c;
149 }
150
151 auto magnitudeInv = 1 / (4 * std::sqrt (magnitude));
152
153 FloatVectorOperations::multiply (coefs, magnitudeInv, static_cast<int> (n));
154}
155
156//==============================================================================
157template struct FIR::Coefficients<float>;
158template struct FIR::Coefficients<double>;
159
160} // namespace dsp
161} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
int n
Definition crypt.c:458
return c
Definition crypt.c:175