1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
28 #include "DynamicList.H"
30 #include "mathematicalConstants.H"
33 // * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * //
35 Foam::scalar Foam::noiseFFT::p0 = 2e-5;
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 // Construct from pressure field
40 Foam::noiseFFT::noiseFFT
43 const scalarField& pressure
46 scalarField(pressure),
51 // Construct from pressure field file name
52 Foam::noiseFFT::noiseFFT(const fileName& pFileName, const label skip)
57 // Construct control dictionary
58 IFstream pFile(pFileName);
60 // Check pFile stream is OK
65 "noiseFFT::noiseFFT(const fileName& pFileName, const label skip)"
66 ) << "Cannot read file " << pFileName
72 scalar dummyt, dummyp;
74 for (label i=0; i<skip; i++)
78 if (!pFile.good() || pFile.eof())
82 "noiseFFT::noiseFFT(const fileName& pFileName, "
84 ) << "Number of points in file " << pFileName
85 << " is less than the number to be skipped = " << skip
94 DynamicList<scalar> pData(100000);
97 while (!(pFile >> t).eof())
103 deltat_ = T/pData.size();
105 scalarField::operator=(pData.shrink());
109 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 Foam::graph Foam::noiseFFT::pt() const
113 scalarField t(size());
130 Foam::tmp<Foam::scalarField> Foam::noiseFFT::window
136 label windowOffset = N;
138 if ((N + ni*windowOffset) > size())
140 FatalErrorIn("noiseFFT::window(const label N, const label n) const")
141 << "Requested window is outside set of data" << endl
142 << "number of data = " << size() << endl
143 << "size of window = " << N << endl
148 tmp<scalarField> tpw(new scalarField(N));
149 scalarField& pw = tpw();
151 label offset = ni*windowOffset;
155 pw[i] = operator[](i + offset);
162 Foam::tmp<Foam::scalarField> Foam::noiseFFT::Hanning(const label N) const
170 scalar T = N*deltat_;
172 return 2*(0.5 - 0.5*cos(2*mathematicalConstant::pi*t/T));
176 Foam::tmp<Foam::scalarField> Foam::noiseFFT::Pf
178 const tmp<scalarField>& tpn
181 tmp<scalarField> tPn2
185 fft::reverseTransform
188 labelList(1, tpn().size())
199 scalarField::subField(tPn2(), tPn2().size()/2)
202 scalarField& Pn = tPn();
204 Pn *= 2.0/sqrt(scalar(tPn2().size()));
211 Foam::graph Foam::noiseFFT::meanPf
219 FatalErrorIn("noiseFFT::meanPf(const label N, const label nw) const")
220 << "Requested window is outside set of data" << endl
221 << "number of data = " << size() << endl
222 << "size of window = " << N << endl
227 scalarField MeanPf(N/2, 0.0);
229 scalarField Hwf = Hanning(N);
231 for (label wi=0; wi<nw; wi++)
233 MeanPf += Pf(Hwf*window(N, wi));
238 scalarField f(MeanPf.size());
239 scalar deltaf = 1.0/(N*deltat_);
257 Foam::graph Foam::noiseFFT::RMSmeanPf
265 FatalErrorIn("noiseFFT::RMSmeanPf(const label N, const label nw) const")
266 << "Requested window is outside set of data" << endl
267 << "number of data = " << size() << endl
268 << "size of window = " << N << endl
273 scalarField RMSMeanPf(N/2, 0.0);
275 scalarField Hwf = Hanning(N);
277 for (label wi=0; wi<nw; wi++)
279 RMSMeanPf += sqr(Pf(Hwf*window(N, wi)));
282 RMSMeanPf = sqrt(RMSMeanPf/nw);
284 scalarField f(RMSMeanPf.size());
285 scalar deltaf = 1.0/(N*deltat_);
303 Foam::graph Foam::noiseFFT::Lf(const graph& gPf) const
316 Foam::graph Foam::noiseFFT::Ldelta
323 const scalarField& f = gLf.x();
324 const scalarField& Lf = gLf.y();
326 scalarField ldelta(Lf.size(), 0.0);
327 scalarField fm(ldelta.size());
329 scalar fratio = cbrt(2.0);
330 scalar deltaf = 1.0/(2*Lf.size()*deltat_);
332 scalar fl = f1/sqrt(fratio);
333 scalar fu = fratio*fl;
335 label istart = label(fl/deltaf);
338 for (label i = istart; i<Lf.size(); i++)
340 scalar fmi = sqrt(fu*fl);
342 if (fmi > fU + 1) break;
347 ldelta[j] = 10*log10(ldelta[j]);
355 ldelta[j] += pow(10, Lf[i]/10.0);
372 Foam::graph Foam::noiseFFT::Pdelta
379 const scalarField& f = gPf.x();
380 const scalarField& Pf = gPf.y();
382 scalarField pdelta(Pf.size(), 0.0);
383 scalarField fm(pdelta.size());
385 scalar fratio = cbrt(2.0);
386 scalar deltaf = 1.0/(2*Pf.size()*deltat_);
388 scalar fl = f1/sqrt(fratio);
389 scalar fu = fratio*fl;
391 label istart = label(fl/deltaf + 1.0 - SMALL);
394 for (label i = istart; i<Pf.size(); i++)
396 scalar fmi = sqrt(fu*fl);
398 if (fmi > fU + 1) break;
403 pdelta[j] = sqrt((2.0/3.0)*pdelta[j]);
411 pdelta[j] += sqr(Pf[i]);
428 Foam::scalar Foam::noiseFFT::Lsum(const graph& gLf) const
430 const scalarField& Lf = gLf.y();
436 lsum += pow(10, Lf[i]/10.0);
439 lsum = 10*log10(lsum);
445 Foam::scalar Foam::noiseFFT::dbToPa(const scalar db) const
447 return p0*pow(10.0, db/20.0);
451 Foam::tmp<Foam::scalarField> Foam::noiseFFT::dbToPa
453 const tmp<scalarField>& db
456 return p0*pow(10.0, db/20.0);
460 // ************************************************************************* //