OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

noiseFFT.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "noiseFFT.H"
00028 #include "IFstream.H"
00029 #include "DynamicList.H"
00030 #include "fft.H"
00031 #include "mathematicalConstants.H"
00032 #include "SubField.H"
00033 
00034 // * * * * * * * * * * * * * * Static Member Data  * * * * * * * * * * * * * //
00035 
00036 Foam::scalar Foam::noiseFFT::p0 = 2e-5;
00037 
00038 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00039 
00040 // Construct from pressure field
00041 Foam::noiseFFT::noiseFFT
00042 (
00043     const scalar deltat,
00044     const scalarField& pressure
00045 )
00046 :
00047     scalarField(pressure),
00048     deltat_(deltat)
00049 {}
00050 
00051 
00052 // Construct from pressure field file name
00053 Foam::noiseFFT::noiseFFT(const fileName& pFileName, const label skip)
00054 :
00055     scalarField(),
00056     deltat_(0.0)
00057 {
00058     // Construct control dictionary
00059     IFstream pFile(pFileName);
00060 
00061     // Check pFile stream is OK
00062     if (!pFile.good())
00063     {
00064         FatalErrorIn
00065         (
00066             "noiseFFT::noiseFFT(const fileName& pFileName, const label skip)"
00067         )   << "Cannot read file " << pFileName
00068             << exit(FatalError);
00069     }
00070 
00071     if (skip)
00072     {
00073         scalar dummyt, dummyp;
00074 
00075         for (label i=0; i<skip; i++)
00076         {
00077             pFile >> dummyt;
00078 
00079             if (!pFile.good() || pFile.eof())
00080             {
00081                 FatalErrorIn
00082                 (
00083                     "noiseFFT::noiseFFT(const fileName& pFileName, "
00084                     "const label skip)"
00085                 )   << "Number of points in file " << pFileName
00086                     << " is less than the number to be skipped = " << skip
00087                     << exit(FatalError);
00088             }
00089 
00090             pFile >> dummyp;
00091         }
00092     }
00093 
00094     scalar t = 0, T = 0;
00095     DynamicList<scalar> pData(100000);
00096     label i = 0;
00097 
00098     while (!(pFile >> t).eof())
00099     {
00100         T = t;
00101         pFile >> pData(i++);
00102     }
00103 
00104     deltat_ = T/pData.size();
00105 
00106     scalarField::operator=(pData.shrink());
00107 }
00108 
00109 
00110 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00111 
00112 Foam::graph Foam::noiseFFT::pt() const
00113 {
00114     scalarField t(size());
00115     forAll (t, i)
00116     {
00117         t[i] = i*deltat_;
00118     }
00119 
00120     return graph
00121     (
00122         "p(t)",
00123         "t [s]",
00124         "p(t) [Pa]",
00125         t,
00126         *this
00127     );
00128 }
00129 
00130 
00131 Foam::tmp<Foam::scalarField> Foam::noiseFFT::window
00132 (
00133     const label N,
00134     const label ni
00135 ) const
00136 {
00137     label windowOffset = N;
00138 
00139     if ((N + ni*windowOffset) > size())
00140     {
00141         FatalErrorIn("noiseFFT::window(const label N, const label n) const")
00142             << "Requested window is outside set of data" << endl
00143             << "number of data = " << size() << endl
00144             << "size of window = " << N << endl
00145             << "window  = " << ni
00146             << exit(FatalError);
00147     }
00148 
00149     tmp<scalarField> tpw(new scalarField(N));
00150     scalarField& pw = tpw();
00151 
00152     label offset = ni*windowOffset;
00153 
00154     forAll (pw, i)
00155     {
00156         pw[i] = operator[](i + offset);
00157     }
00158 
00159     return tpw;
00160 }
00161 
00162 
00163 Foam::tmp<Foam::scalarField> Foam::noiseFFT::Hanning(const label N) const
00164 {
00165     scalarField t(N);
00166     forAll (t, i)
00167     {
00168         t[i] = i*deltat_;
00169     }
00170 
00171     scalar T = N*deltat_;
00172 
00173     return 2*(0.5 - 0.5*cos(2*mathematicalConstant::pi*t/T));
00174 }
00175 
00176 
00177 Foam::tmp<Foam::scalarField> Foam::noiseFFT::Pf
00178 (
00179     const tmp<scalarField>& tpn
00180 ) const
00181 {
00182     tmp<scalarField> tPn2
00183     (
00184         mag
00185         (
00186             fft::reverseTransform
00187             (
00188                 ReComplexField(tpn),
00189                 labelList(1, tpn().size())
00190             )
00191         )
00192     );
00193 
00194     tpn.clear();
00195 
00196     tmp<scalarField> tPn
00197     (
00198         new scalarField
00199         (
00200             scalarField::subField(tPn2(), tPn2().size()/2)
00201         )
00202     );
00203     scalarField& Pn = tPn();
00204 
00205     Pn *= 2.0/sqrt(scalar(tPn2().size()));
00206     Pn[0] /= 2.0;
00207 
00208     return tPn;
00209 }
00210 
00211 
00212 Foam::graph Foam::noiseFFT::meanPf
00213 (
00214     const label N,
00215     const label nw
00216 ) const
00217 {
00218     if (N > size())
00219     {
00220         FatalErrorIn("noiseFFT::meanPf(const label N, const label nw) const")
00221             << "Requested window is outside set of data" << endl
00222             << "number of data = " << size() << endl
00223             << "size of window = " << N << endl
00224             << "window  = " << nw
00225             << exit(FatalError);
00226     }
00227 
00228     scalarField MeanPf(N/2, 0.0);
00229 
00230     scalarField Hwf = Hanning(N);
00231 
00232     for (label wi=0; wi<nw; wi++)
00233     {
00234         MeanPf += Pf(Hwf*window(N, wi));
00235     }
00236 
00237     MeanPf /= nw;
00238 
00239     scalarField f(MeanPf.size());
00240     scalar deltaf = 1.0/(N*deltat_);
00241 
00242     forAll (f, i)
00243     {
00244         f[i] = i*deltaf;
00245     }
00246 
00247     return graph
00248     (
00249         "P(f)",
00250         "f [Hz]",
00251         "P(f) [Pa]",
00252         f,
00253         MeanPf
00254     );
00255 }
00256 
00257 
00258 Foam::graph Foam::noiseFFT::RMSmeanPf
00259 (
00260     const label N,
00261     const label nw
00262 ) const
00263 {
00264     if (N > size())
00265     {
00266         FatalErrorIn("noiseFFT::RMSmeanPf(const label N, const label nw) const")
00267             << "Requested window is outside set of data" << endl
00268             << "number of data = " << size() << endl
00269             << "size of window = " << N << endl
00270             << "window  = " << nw
00271             << exit(FatalError);
00272     }
00273 
00274     scalarField RMSMeanPf(N/2, 0.0);
00275 
00276     scalarField Hwf = Hanning(N);
00277 
00278     for (label wi=0; wi<nw; wi++)
00279     {
00280         RMSMeanPf += sqr(Pf(Hwf*window(N, wi)));
00281     }
00282 
00283     RMSMeanPf = sqrt(RMSMeanPf/nw);
00284 
00285     scalarField f(RMSMeanPf.size());
00286     scalar deltaf = 1.0/(N*deltat_);
00287 
00288     forAll (f, i)
00289     {
00290         f[i] = i*deltaf;
00291     }
00292 
00293     return graph
00294     (
00295         "P(f)",
00296         "f [Hz]",
00297         "P(f) [Pa]",
00298         f,
00299         RMSMeanPf
00300     );
00301 }
00302 
00303 
00304 Foam::graph Foam::noiseFFT::Lf(const graph& gPf) const
00305 {
00306     return graph
00307     (
00308         "L(f)",
00309         "f [Hz]",
00310         "L(f) [dB]",
00311         gPf.x(),
00312         20*log10(gPf.y()/p0)
00313     );
00314 }
00315 
00316 
00317 Foam::graph Foam::noiseFFT::Ldelta
00318 (
00319     const graph& gLf,
00320     const scalar f1,
00321     const scalar fU
00322 ) const
00323 {
00324     const scalarField& f = gLf.x();
00325     const scalarField& Lf = gLf.y();
00326 
00327     scalarField ldelta(Lf.size(), 0.0);
00328     scalarField fm(ldelta.size());
00329 
00330     scalar fratio = cbrt(2.0);
00331     scalar deltaf = 1.0/(2*Lf.size()*deltat_);
00332 
00333     scalar fl = f1/sqrt(fratio);
00334     scalar fu = fratio*fl;
00335 
00336     label istart = label(fl/deltaf);
00337     label j = 0;
00338 
00339     for (label i = istart; i<Lf.size(); i++)
00340     {
00341         scalar fmi = sqrt(fu*fl);
00342 
00343         if (fmi > fU + 1) break;
00344 
00345         if (f[i] >= fu)
00346         {
00347             fm[j] = fmi;
00348             ldelta[j] = 10*log10(ldelta[j]);
00349 
00350             j++;
00351 
00352             fl = fu;
00353             fu *= fratio;
00354         }
00355 
00356         ldelta[j] += pow(10, Lf[i]/10.0);
00357     }
00358 
00359     fm.setSize(j);
00360     ldelta.setSize(j);
00361 
00362     return graph
00363     (
00364         "Ldelta",
00365         "fm [Hz]",
00366         "Ldelta [dB]",
00367         fm,
00368         ldelta
00369     );
00370 }
00371 
00372 
00373 Foam::graph Foam::noiseFFT::Pdelta
00374 (
00375     const graph& gPf,
00376     const scalar f1,
00377     const scalar fU
00378 ) const
00379 {
00380     const scalarField& f = gPf.x();
00381     const scalarField& Pf = gPf.y();
00382 
00383     scalarField pdelta(Pf.size(), 0.0);
00384     scalarField fm(pdelta.size());
00385 
00386     scalar fratio = cbrt(2.0);
00387     scalar deltaf = 1.0/(2*Pf.size()*deltat_);
00388 
00389     scalar fl = f1/sqrt(fratio);
00390     scalar fu = fratio*fl;
00391 
00392     label istart = label(fl/deltaf + 1.0 - SMALL);
00393     label j = 0;
00394 
00395     for (label i = istart; i<Pf.size(); i++)
00396     {
00397         scalar fmi = sqrt(fu*fl);
00398 
00399         if (fmi > fU + 1) break;
00400 
00401         if (f[i] >= fu)
00402         {
00403             fm[j] = fmi;
00404             pdelta[j] = sqrt((2.0/3.0)*pdelta[j]);
00405 
00406             j++;
00407 
00408             fl = fu;
00409             fu *= fratio;
00410         }
00411 
00412         pdelta[j] += sqr(Pf[i]);
00413     }
00414 
00415     fm.setSize(j);
00416     pdelta.setSize(j);
00417 
00418     return graph
00419     (
00420         "Pdelta",
00421         "fm [Hz]",
00422         "Pdelta [dB]",
00423         fm,
00424         pdelta
00425     );
00426 }
00427 
00428 
00429 Foam::scalar Foam::noiseFFT::Lsum(const graph& gLf) const
00430 {
00431     const scalarField& Lf = gLf.y();
00432 
00433     scalar lsum = 0.0;
00434 
00435     forAll(Lf, i)
00436     {
00437         lsum += pow(10, Lf[i]/10.0);
00438     }
00439 
00440     lsum = 10*log10(lsum);
00441 
00442     return lsum;
00443 }
00444 
00445 
00446 Foam::scalar Foam::noiseFFT::dbToPa(const scalar db) const
00447 {
00448     return p0*pow(10.0, db/20.0);
00449 }
00450 
00451 
00452 Foam::tmp<Foam::scalarField> Foam::noiseFFT::dbToPa
00453 (
00454     const tmp<scalarField>& db
00455 ) const
00456 {
00457     return p0*pow(10.0, db/20.0);
00458 }
00459 
00460 
00461 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd