Line data Source code
1 : /** 2 : * \file RobertBristowJohnsonFilter.hxx 3 : */ 4 : 5 : #include "RobertBristowJohnsonFilter.h" 6 : 7 : #include <boost/math/constants/constants.hpp> 8 : 9 : namespace ATK 10 : { 11 : template<typename DataType> 12 6 : RobertBristowJohnsonLowPassCoefficients<DataType>::RobertBristowJohnsonLowPassCoefficients(gsl::index nb_channels) 13 6 : :Parent(nb_channels) 14 : { 15 6 : } 16 : 17 : template <typename DataType> 18 13 : void RobertBristowJohnsonLowPassCoefficients<DataType>::setup() 19 : { 20 13 : Parent::setup(); 21 : 22 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 23 13 : CoeffDataType cosw0 = std::cos(w0); 24 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 25 : 26 13 : coefficients_in[2] = (1 - cosw0)/2 / (1 + alpha); 27 13 : coefficients_in[1] = (1 - cosw0) / (1 + alpha); 28 13 : coefficients_in[0] = (1 - cosw0) / 2 / (1 + alpha); 29 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 30 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 31 13 : } 32 : 33 : template <typename DataType_> 34 2 : void RobertBristowJohnsonLowPassCoefficients<DataType_>::set_Q(CoeffDataType Q) 35 : { 36 2 : if (Q <= 0) 37 : { 38 1 : throw std::out_of_range("Q must be positive"); 39 : } 40 1 : this->Q = Q; 41 1 : setup(); 42 1 : } 43 : 44 : template <typename DataType_> 45 1 : typename RobertBristowJohnsonLowPassCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonLowPassCoefficients<DataType_>::get_Q() const 46 : { 47 1 : return Q; 48 : } 49 : 50 : template<typename DataType> 51 6 : RobertBristowJohnsonHighPassCoefficients<DataType>::RobertBristowJohnsonHighPassCoefficients(gsl::index nb_channels) 52 6 : :Parent(nb_channels) 53 : { 54 6 : } 55 : 56 : template <typename DataType> 57 13 : void RobertBristowJohnsonHighPassCoefficients<DataType>::setup() 58 : { 59 13 : Parent::setup(); 60 : 61 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 62 13 : CoeffDataType cosw0 = std::cos(w0); 63 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 64 : 65 13 : coefficients_in[2] = (1 + cosw0) / 2 / (1 + alpha); 66 13 : coefficients_in[1] = -(1 + cosw0) / (1 + alpha); 67 13 : coefficients_in[0] = (1 + cosw0) / 2 / (1 + alpha); 68 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 69 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 70 13 : } 71 : 72 : template <typename DataType_> 73 2 : void RobertBristowJohnsonHighPassCoefficients<DataType_>::set_Q(CoeffDataType Q) 74 : { 75 2 : if (Q <= 0) 76 : { 77 1 : throw std::out_of_range("Q must be positive"); 78 : } 79 1 : this->Q = Q; 80 1 : setup(); 81 1 : } 82 : 83 : template <typename DataType_> 84 1 : typename RobertBristowJohnsonHighPassCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonHighPassCoefficients<DataType_>::get_Q() const 85 : { 86 1 : return Q; 87 : } 88 : 89 : template<typename DataType> 90 5 : RobertBristowJohnsonBandPassCoefficients<DataType>::RobertBristowJohnsonBandPassCoefficients(gsl::index nb_channels) 91 5 : :Parent(nb_channels) 92 : { 93 5 : } 94 : 95 : template <typename DataType> 96 13 : void RobertBristowJohnsonBandPassCoefficients<DataType>::setup() 97 : { 98 13 : Parent::setup(); 99 : 100 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 101 13 : CoeffDataType cosw0 = std::cos(w0); 102 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 103 : 104 13 : coefficients_in[2] = Q * alpha / (1 + alpha); 105 13 : coefficients_in[1] = 0; 106 13 : coefficients_in[0] = - Q * alpha / (1 + alpha); 107 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 108 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 109 13 : } 110 : 111 : template <typename DataType_> 112 5 : void RobertBristowJohnsonBandPassCoefficients<DataType_>::set_Q(CoeffDataType Q) 113 : { 114 5 : if (Q <= 0) 115 : { 116 1 : throw std::out_of_range("Q must be positive"); 117 : } 118 4 : this->Q = Q; 119 4 : setup(); 120 4 : } 121 : 122 : template <typename DataType_> 123 1 : typename RobertBristowJohnsonBandPassCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonBandPassCoefficients<DataType_>::get_Q() const 124 : { 125 1 : return Q; 126 : } 127 : 128 : template<typename DataType> 129 5 : RobertBristowJohnsonBandPass2Coefficients<DataType>::RobertBristowJohnsonBandPass2Coefficients(gsl::index nb_channels) 130 5 : :Parent(nb_channels) 131 : { 132 5 : } 133 : 134 : template <typename DataType> 135 13 : void RobertBristowJohnsonBandPass2Coefficients<DataType>::setup() 136 : { 137 13 : Parent::setup(); 138 : 139 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 140 13 : CoeffDataType cosw0 = std::cos(w0); 141 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 142 : 143 13 : coefficients_in[2] = alpha / (1 + alpha); 144 13 : coefficients_in[1] = 0; 145 13 : coefficients_in[0] = -alpha / (1 + alpha); 146 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 147 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 148 13 : } 149 : 150 : template <typename DataType_> 151 5 : void RobertBristowJohnsonBandPass2Coefficients<DataType_>::set_Q(CoeffDataType Q) 152 : { 153 5 : if (Q <= 0) 154 : { 155 1 : throw std::out_of_range("Q must be positive"); 156 : } 157 4 : this->Q = Q; 158 4 : setup(); 159 4 : } 160 : 161 : template <typename DataType_> 162 1 : typename RobertBristowJohnsonBandPass2Coefficients<DataType_>::CoeffDataType RobertBristowJohnsonBandPass2Coefficients<DataType_>::get_Q() const 163 : { 164 1 : return Q; 165 : } 166 : 167 : template<typename DataType> 168 5 : RobertBristowJohnsonBandStopCoefficients<DataType>::RobertBristowJohnsonBandStopCoefficients(gsl::index nb_channels) 169 5 : :Parent(nb_channels) 170 : { 171 5 : } 172 : 173 : template <typename DataType> 174 13 : void RobertBristowJohnsonBandStopCoefficients<DataType>::setup() 175 : { 176 13 : Parent::setup(); 177 : 178 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 179 13 : CoeffDataType cosw0 = std::cos(w0); 180 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 181 : 182 13 : coefficients_in[2] = 1 / (1 + alpha); 183 13 : coefficients_in[1] = -2 * cosw0 / (1 + alpha); 184 13 : coefficients_in[0] = 1 / (1 + alpha); 185 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 186 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 187 13 : } 188 : 189 : template <typename DataType_> 190 5 : void RobertBristowJohnsonBandStopCoefficients<DataType_>::set_Q(CoeffDataType Q) 191 : { 192 5 : if (Q <= 0) 193 : { 194 1 : throw std::out_of_range("Q must be positive"); 195 : } 196 4 : this->Q = Q; 197 4 : setup(); 198 4 : } 199 : 200 : template <typename DataType_> 201 1 : typename RobertBristowJohnsonBandStopCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonBandStopCoefficients<DataType_>::get_Q() const 202 : { 203 1 : return Q; 204 : } 205 : 206 : template<typename DataType> 207 5 : RobertBristowJohnsonAllPassCoefficients<DataType>::RobertBristowJohnsonAllPassCoefficients(gsl::index nb_channels) 208 5 : :Parent(nb_channels) 209 : { 210 5 : } 211 : 212 : template <typename DataType> 213 13 : void RobertBristowJohnsonAllPassCoefficients<DataType>::setup() 214 : { 215 13 : Parent::setup(); 216 : 217 13 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 218 13 : CoeffDataType cosw0 = std::cos(w0); 219 13 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 220 : 221 13 : coefficients_in[2] = (1 - alpha) / (1 + alpha); 222 13 : coefficients_in[1] = -2 * cosw0 / (1 + alpha); 223 13 : coefficients_in[0] = 1; 224 13 : coefficients_out[1] = 2 * cosw0 / (1 + alpha); 225 13 : coefficients_out[0] = (alpha - 1) / (1 + alpha); 226 13 : } 227 : 228 : template <typename DataType_> 229 5 : void RobertBristowJohnsonAllPassCoefficients<DataType_>::set_Q(CoeffDataType Q) 230 : { 231 5 : if (Q <= 0) 232 : { 233 1 : throw std::out_of_range("Q must be positive"); 234 : } 235 4 : this->Q = Q; 236 4 : setup(); 237 4 : } 238 : 239 : template <typename DataType_> 240 1 : typename RobertBristowJohnsonAllPassCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonAllPassCoefficients<DataType_>::get_Q() const 241 : { 242 1 : return Q; 243 : } 244 : 245 : template<typename DataType> 246 7 : RobertBristowJohnsonBandPassPeakCoefficients<DataType>::RobertBristowJohnsonBandPassPeakCoefficients(gsl::index nb_channels) 247 7 : :Parent(nb_channels) 248 : { 249 7 : } 250 : 251 : template <typename DataType> 252 17 : void RobertBristowJohnsonBandPassPeakCoefficients<DataType>::setup() 253 : { 254 17 : Parent::setup(); 255 : 256 17 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 257 17 : CoeffDataType cosw0 = std::cos(w0); 258 17 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 259 : 260 17 : coefficients_in[2] = (1 + alpha * gain) / (1 + alpha / gain); 261 17 : coefficients_in[1] = -2 * cosw0 / (1 + alpha / gain); 262 17 : coefficients_in[0] = (1 - alpha * gain) / (1 + alpha / gain); 263 17 : coefficients_out[1] = 2 * cosw0 / (1 + alpha / gain); 264 17 : coefficients_out[0] = (alpha / gain - 1) / (1 + alpha / gain); 265 17 : } 266 : 267 : template <typename DataType_> 268 5 : void RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::set_Q(CoeffDataType Q) 269 : { 270 5 : if (Q <= 0) 271 : { 272 1 : throw std::out_of_range("Q must be positive"); 273 : } 274 4 : this->Q = Q; 275 4 : setup(); 276 4 : } 277 : 278 : template <typename DataType_> 279 1 : typename RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::get_Q() const 280 : { 281 1 : return Q; 282 : } 283 : 284 : template <typename DataType_> 285 5 : void RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::set_gain(CoeffDataType gain) 286 : { 287 5 : if (gain <= 0) 288 : { 289 1 : throw std::out_of_range("gain must be positive"); 290 : } 291 4 : this->gain = gain; 292 4 : setup(); 293 4 : } 294 : 295 : template <typename DataType_> 296 1 : typename RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonBandPassPeakCoefficients<DataType_>::get_gain() const 297 : { 298 1 : return gain; 299 : } 300 : 301 : template<typename DataType> 302 7 : RobertBristowJohnsonLowShelvingCoefficients<DataType>::RobertBristowJohnsonLowShelvingCoefficients(gsl::index nb_channels) 303 7 : :Parent(nb_channels) 304 : { 305 7 : } 306 : 307 : template <typename DataType> 308 14 : void RobertBristowJohnsonLowShelvingCoefficients<DataType>::setup() 309 : { 310 14 : Parent::setup(); 311 : 312 14 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 313 14 : CoeffDataType cosw0 = std::cos(w0); 314 14 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 315 14 : CoeffDataType d = (gain + 1) + (gain - 1) * cosw0 + 2 * std::sqrt(gain) * alpha; 316 : 317 14 : coefficients_in[2] = gain * ((gain + 1) - (gain - 1) * cosw0 + 2 * sqrt(gain) * alpha) / d; 318 14 : coefficients_in[1] = 2 * gain * ((gain - 1) - (gain + 1) * cosw0) / d; 319 14 : coefficients_in[0] = gain * ((gain + 1) - (gain - 1) * cosw0 - 2 * sqrt(gain) * alpha) / d; 320 14 : coefficients_out[1] = 2 * ((gain - 1) + (gain + 1) * cosw0) / d; 321 14 : coefficients_out[0] = -((gain + 1) + (gain - 1) * cosw0 - 2 * sqrt(gain) * alpha) / d; 322 14 : } 323 : 324 : template <typename DataType_> 325 2 : void RobertBristowJohnsonLowShelvingCoefficients<DataType_>::set_Q(CoeffDataType Q) 326 : { 327 2 : if (Q <= 0) 328 : { 329 1 : throw std::out_of_range("Q must be positive"); 330 : } 331 1 : this->Q = Q; 332 1 : setup(); 333 1 : } 334 : 335 : template <typename DataType_> 336 1 : typename RobertBristowJohnsonLowShelvingCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonLowShelvingCoefficients<DataType_>::get_Q() const 337 : { 338 1 : return Q; 339 : } 340 : 341 : template <typename DataType_> 342 5 : void RobertBristowJohnsonLowShelvingCoefficients<DataType_>::set_gain(CoeffDataType gain) 343 : { 344 5 : if (gain <= 0) 345 : { 346 1 : throw std::out_of_range("gain must be positive"); 347 : } 348 4 : this->gain = gain; 349 4 : setup(); 350 4 : } 351 : 352 : template <typename DataType_> 353 1 : typename RobertBristowJohnsonLowShelvingCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonLowShelvingCoefficients<DataType_>::get_gain() const 354 : { 355 1 : return gain; 356 : } 357 : 358 : template<typename DataType> 359 7 : RobertBristowJohnsonHighShelvingCoefficients<DataType>::RobertBristowJohnsonHighShelvingCoefficients(gsl::index nb_channels) 360 7 : :Parent(nb_channels) 361 : { 362 7 : } 363 : 364 : template <typename DataType> 365 14 : void RobertBristowJohnsonHighShelvingCoefficients<DataType>::setup() 366 : { 367 14 : Parent::setup(); 368 : 369 14 : CoeffDataType w0 = 2 * boost::math::constants::pi<CoeffDataType>() * cut_frequency / input_sampling_rate; 370 14 : CoeffDataType cosw0 = std::cos(w0); 371 14 : CoeffDataType alpha = std::sin(w0) / (2 * Q); 372 14 : CoeffDataType d = (gain + 1) - (gain - 1) * cosw0 + 2 * std::sqrt(gain) * alpha; 373 : 374 14 : coefficients_in[2] = gain * ((gain + 1) + (gain - 1) * cosw0 + 2 * sqrt(gain) * alpha) / d; 375 14 : coefficients_in[1] = -2 * gain * ((gain - 1) + (gain + 1) * cosw0) / d; 376 14 : coefficients_in[0] = gain * ((gain + 1) + (gain - 1) * cosw0 - 2 * sqrt(gain) * alpha) / d; 377 14 : coefficients_out[1] = -2 * ((gain - 1) - (gain + 1) * cosw0) / d; 378 14 : coefficients_out[0] = -((gain + 1) - (gain - 1) * cosw0 - 2 * sqrt(gain) * alpha) / d; 379 14 : } 380 : 381 : template <typename DataType_> 382 2 : void RobertBristowJohnsonHighShelvingCoefficients<DataType_>::set_Q(CoeffDataType Q) 383 : { 384 2 : if (Q <= 0) 385 : { 386 1 : throw std::out_of_range("Q must be positive"); 387 : } 388 1 : this->Q = Q; 389 1 : setup(); 390 1 : } 391 : 392 : template <typename DataType_> 393 1 : typename RobertBristowJohnsonHighShelvingCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonHighShelvingCoefficients<DataType_>::get_Q() const 394 : { 395 1 : return Q; 396 : } 397 : 398 : template <typename DataType_> 399 5 : void RobertBristowJohnsonHighShelvingCoefficients<DataType_>::set_gain(CoeffDataType gain) 400 : { 401 5 : if (gain <= 0) 402 : { 403 1 : throw std::out_of_range("gain must be positive"); 404 : } 405 4 : this->gain = gain; 406 4 : setup(); 407 4 : } 408 : 409 : template <typename DataType_> 410 1 : typename RobertBristowJohnsonHighShelvingCoefficients<DataType_>::CoeffDataType RobertBristowJohnsonHighShelvingCoefficients<DataType_>::get_gain() const 411 : { 412 1 : return gain; 413 : } 414 : }