-
Notifications
You must be signed in to change notification settings - Fork 5
feature/cavity_noise_simulation (additive noise implementation) #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ehtkarim
wants to merge
14
commits into
develop
Choose a base branch
from
feature/cavity_noise_simulation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
81000d6
Initial commmit for implementing working aWGN in Katydid, need to set…
ehtkarim 22c4938
Added a fix for handling noise addition properly (resolves mirrored s…
ehtkarim a12bb55
Added a way to incorporate seed for the RNG()
ehtkarim de31c1e
Added an example config file for applying aWGN
ehtkarim 8b0b99d
Minor fix in KTRandom.hh
ehtkarim 8265bdd
Removed commits and made minor changes before merging to /develop
ehtkarim 33a5215
Minor indentation fix
ehtkarim ac7f484
Initial implementation of cavity noise model under KTCavityNoiseGener…
ehtkarim 8b0f5a3
Fixed cavity parameters reading-in, f_lo implementation & complex sig…
ehtkarim 9be0933
Added feature to set aWGN by noise-floor-psd or noise-temperature, an…
ehtkarim 62dcc25
Added documentation for new processors, and created 'KTMeasuredNoiseG…
ehtkarim 4a93ba5
Correcting power normalization KTMeasuredNoiseGenerator.cc
ehtkarim 5ca8f6c
Renaming variables and member functions in Source/Simulation/KTCavity…
ehtkarim 442cebf
Removed ROOT dependency from KTSpline, fixed KTMeasuredNoiseGenerator…
ehtkarim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| processor-toolbox: | ||
| processors: | ||
| - type: egg-processor | ||
| name: egg | ||
| - type: forward-fftw | ||
| name: fft | ||
| - type: cavity-noise-generator | ||
| name: noise-gen | ||
| - type: convert-to-power | ||
| name: to-ps | ||
| - type: basic-root-writer | ||
| name: writer | ||
|
|
||
| connections: | ||
| - signal: "egg:header" | ||
| slot: "fft:header" | ||
| - signal: "egg:ts" | ||
| slot: "noise-gen:slice" | ||
| - signal: "noise-gen:slice" | ||
| slot: "fft:ts-fftw" | ||
| - signal: "fft:fft" | ||
| slot: "to-ps:fs-fftw-to-psd" | ||
| - signal: "to-ps:psd" | ||
| slot: "writer:ps" | ||
| - signal: "egg:ts" | ||
| slot: "writer:ts" | ||
|
|
||
| run-queue: | ||
| - egg | ||
|
|
||
| egg: | ||
| filename: "/path/to/file.root" | ||
| egg-reader: egg3 | ||
| slice-size: 8192 | ||
| number-of-slices: 1 | ||
|
|
||
| fft: | ||
| transform-flag: ESTIMATE | ||
|
|
||
| noise-gen: | ||
| seed: 12345 | ||
| transform-flag: ESTIMATE | ||
| #noise-scaling: 1.0 # noise amplitude scaling | ||
| #cavity: # for CCA cavity, tune as needed | ||
| # f0 : 25.904e9 | ||
| # Q_L : 625 | ||
| # Q0 : 10000 | ||
| # A : 0.90 | ||
| # T_line_start : 80.0 | ||
| # T_line_end : 5.2 | ||
| # T_cav : 80.0 | ||
| # T_isol : 5.2 | ||
| # epsilon : 0.5 | ||
| # f_lo : 25.9702e9 | ||
|
|
||
| writer: | ||
| output-file: "/path/to/file.root" | ||
| file-flag: recreate |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename this to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| processor-toolbox: | ||
| processors: | ||
| - type: egg-processor | ||
| name: egg | ||
| - type: forward-fftw | ||
| name: fft | ||
| - type: gaussian-noise-generator | ||
| name: noise-gen | ||
| - type: convert-to-power | ||
| name: to-ps | ||
| - type: basic-root-writer | ||
| name: writer | ||
|
|
||
| connections: | ||
| - signal: "egg:header" | ||
| slot: "fft:header" | ||
| - signal: "egg:ts" | ||
| slot: "noise-gen:slice" | ||
| - signal: "noise-gen:slice" | ||
| slot: "fft:ts-fftw" | ||
| - signal: "fft:fft" | ||
| slot: "to-ps:fs-fftw-to-psd" | ||
| - signal: "to-ps:psd" | ||
| slot: "writer:ps" | ||
| - signal: "egg:ts" | ||
| slot: "writer:ts" | ||
|
|
||
| run-queue: | ||
| - egg | ||
|
|
||
| egg: | ||
| filename: "/path/to/file.root" | ||
| egg-reader: egg3 | ||
| slice-size: 8192 | ||
| number-of-slices: 0 | ||
|
|
||
| fft: | ||
| transform-flag: ESTIMATE | ||
|
|
||
| noise-gen: | ||
| mean: 0.0 | ||
| noise-floor-psd: 2.2e-13 # Noise power in W/Hz | ||
| seed: 12345 | ||
|
|
||
| writer: | ||
| output-file: "/path/to/file.root" | ||
| file-flag: recreate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
|
|
||
| #include "TH1.h" | ||
| #include "TH2.h" | ||
| #include "TGraph.h" | ||
| #include "TGraph2D.h" | ||
|
|
||
| #include <sstream> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /* | ||
| * KTCavityNoiseGenerator.cc | ||
| * | ||
| * Created on: May 28, 2025 | ||
| * Author: ehtkarim | ||
| */ | ||
|
|
||
| #include "KTCavityNoiseGenerator.hh" | ||
|
|
||
| #include "param.hh" | ||
| #include "KTMath.hh" | ||
| #include "KTTimeSeriesData.hh" | ||
| #include "KTTimeSeries.hh" | ||
| #include "KTTimeSeriesFFTW.hh" | ||
| #include "KTFrequencySpectrumFFTW.hh" | ||
| #include "KTReverseFFTW.hh" | ||
|
|
||
| #include <cmath> | ||
| #include <memory> | ||
|
|
||
| using std::string; | ||
|
|
||
| namespace Katydid | ||
| { | ||
| KTLOGGER(genlog, "KTCavityNoiseGenerator"); | ||
|
|
||
| KT_REGISTER_PROCESSOR(KTCavityNoiseGenerator, "cavity-noise-generator"); | ||
|
|
||
| KTCavityNoiseGenerator::KTCavityNoiseGenerator(const string& name) : | ||
| KTGaussianNoiseGenerator(name), | ||
| fF0(25.904e9), | ||
| fQL(625.0), | ||
| fQ0(1.e4), | ||
| fA(0.90), | ||
| fTLineStart(80.0), | ||
| fTLineEnd(5.2), | ||
| fTCav(80.0), | ||
| fTIsol(5.2), | ||
| fEpsilon(0.5), | ||
| fFLo(25.9702e9), | ||
| fTransformFlag("ESTIMATE"), | ||
| fNoiseScaling(1.0) | ||
| { | ||
| } | ||
|
|
||
| KTCavityNoiseGenerator::~KTCavityNoiseGenerator() | ||
| { | ||
| } | ||
|
|
||
| bool KTCavityNoiseGenerator::ConfigureDerivedGenerator(const scarab::param_node* node) | ||
| { | ||
| if (node == NULL) return false; | ||
| if (! KTGaussianNoiseGenerator::ConfigureDerivedGenerator(node)) return false; | ||
|
|
||
| fRNG.param(KTRNGGaussian<>::param_type(0.0, 1.0)); // Cavity noise should have fRNG() with default (mean, sigma), not inherited from KTGaussianNoiseGenerator | ||
|
|
||
| if (node->has("cavity")) | ||
| { | ||
| const scarab::param_node& m = (*node)["cavity"].as_node(); | ||
| fF0 = m.get_value("f0", fF0); | ||
| fQL = m.get_value("Q_L", fQL); | ||
| fQ0 = m.get_value("Q0", fQ0); | ||
| fA = m.get_value("A", fA); | ||
| fTLineStart = m.get_value("T_line_start", fTLineStart); | ||
| fTLineEnd = m.get_value("T_line_end", fTLineEnd); | ||
| fTCav = m.get_value("T_cav", fTCav); | ||
| fTIsol = m.get_value("T_isol", fTIsol); | ||
| fEpsilon = m.get_value("epsilon", fEpsilon); | ||
| fFLo = m.get_value("f_lo", fFLo); | ||
| } | ||
|
|
||
| fNoiseScaling = node->get_value<double>("noise-scaling", fNoiseScaling); | ||
| if (fNoiseScaling <= 0.0) | ||
| { | ||
| KTWARN(genlog, "\"noise-scaling\" must be > 0; using 1.0"); | ||
| fNoiseScaling = 1.0; | ||
| } | ||
|
|
||
| fTransformFlag = node->get_value("transform-flag", fTransformFlag); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool KTCavityNoiseGenerator::GenerateTS(KTTimeSeriesData& data) | ||
| { | ||
| const double binWidth = data.GetTimeSeries(0)->GetTimeBinWidth(); | ||
| const unsigned sliceSize = data.GetTimeSeries(0)->GetNTimeBins(); | ||
| const unsigned nComponents = data.GetNComponents(); | ||
|
|
||
| const double fs = 1.0 / binWidth; | ||
| const double df = fs / sliceSize; | ||
| const unsigned N2 = sliceSize / 2; | ||
|
|
||
| bool isComplex = dynamic_cast< KTTimeSeriesFFTW* >(data.GetTimeSeries(0)) != NULL; | ||
|
|
||
| KTFrequencySpectrumFFTW spec(sliceSize, -fs*0.5, fs*0.5, false); | ||
| spec.SetNTimeBins(sliceSize); | ||
|
|
||
| if (isComplex) | ||
| { | ||
| for (unsigned k = 0; k < sliceSize; ++k) | ||
| { | ||
| double fIf = (k <= N2) ? k * df : (static_cast<int>(k) - static_cast<int>(sliceSize)) * df; | ||
| double fRf = fIf + fFLo; // Down-converted | ||
| double pBin = NoisePSD(fRf) * df; // PSD -> power in one FFT bin | ||
| double amp = fNoiseScaling*fGain*std::sqrt(fResistance)*std::pow(sliceSize, 1.5)*std::sqrt(pBin / 2.0); // N^{3/2}*sqrt(P_bin/2) - N^{3/2} since ReverseFFTW does a sqrt(N) normalization | ||
|
|
||
| spec.SetRect(k, amp * fRNG(), amp * fRNG()); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| for (unsigned k = 0; k <= N2; ++k) | ||
| { | ||
| double fIf = k * df; | ||
| double fRf = fIf + fFLo; // Down-converted | ||
| double pBin = NoisePSD(fRf) * df; // PSD -> power in one FFT bin | ||
| double amp = fNoiseScaling*fGain*std::sqrt(fResistance)*std::pow(sliceSize, 1.5)*std::sqrt(pBin); // N^{3/2}*sqrt(P_bin) - for real signal bins P_bin/2 -> P_bin | ||
|
|
||
| double re = amp * fRNG(); | ||
| double im = (k==0 || (sliceSize%2==0 && k==N2)) ? 0.0 : amp * fRNG(); // Set imag component 0 for the DC bin (k = 0) and for the Nyquist bin (k = N/2) (even); otherwise amp * fRNG() | ||
|
|
||
| spec.SetRect(k, re, im); | ||
| if (k>0 && k<N2) | ||
| spec.SetRect(sliceSize - k, re, -im); | ||
| } | ||
| } | ||
|
|
||
| KTReverseFFTW rfft; | ||
| rfft.SetTransformFlag(fTransformFlag); | ||
| rfft.InitializeForComplexTDD(sliceSize); | ||
|
|
||
| std::unique_ptr< KTTimeSeriesFFTW > noiseTS( rfft.TransformToComplex(&spec) ); | ||
| if (! noiseTS) | ||
| { | ||
| KTERROR(genlog, "Inverse FFT failed while producing cavity noise"); | ||
| return false; | ||
| } | ||
|
|
||
| for (unsigned iComponent = 0; iComponent < nComponents; ++iComponent) | ||
| { | ||
| KTTimeSeries* ts = data.GetTimeSeries(iComponent); | ||
|
|
||
| if (auto* tsFFTW = dynamic_cast< KTTimeSeriesFFTW* >(ts)) | ||
| { | ||
| for (unsigned i = 0; i < sliceSize; ++i) | ||
| tsFFTW->SetRect(i, tsFFTW->GetReal(i) + noiseTS->GetReal(i), tsFFTW->GetImag(i) + noiseTS->GetImag(i)); | ||
| } | ||
| else | ||
| { | ||
| for (unsigned i = 0; i < sliceSize; ++i) | ||
| ts->SetValue(i, ts->GetValue(i) + noiseTS->GetReal(i)); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| double KTCavityNoiseGenerator::Eta(double x) const | ||
| { | ||
| return x/(std::exp(x)-1.0) + 0.5; | ||
| } | ||
|
|
||
| double KTCavityNoiseGenerator::NoisePSD(double f) const | ||
| { | ||
| const double lor = 1.0 / ( 1.0 + ( 2.0 * fQL * (f - fF0) / fF0)*( 2.0 * fQL * (f - fF0) / fF0) ); // Lorentzian | ||
|
|
||
| constexpr double kB = KTMath::BoltzmannConstant(); | ||
| constexpr double hbar= KTMath::ReducedPlanckConstant(); | ||
| const double omega = KTMath::TwoPi() * f; | ||
|
|
||
| const double Tcav = fTCav * Eta(hbar*omega/(kB*fTCav)); | ||
| const double Tisol = fTIsol * Eta(hbar*omega/(kB*fTIsol)); | ||
|
|
||
| const double P_cav = kB*Tcav * (4.*fG/((1.+fG)*(1.+fG))) * lor; | ||
| const double P_loss = kB*( fA*fTLineStart + (1.-fA)*fTLineEnd ); | ||
| const double P_isol = kB*Tisol * (1. - (4.*fG/((1.+fG)*(1.+fG)))*lor); | ||
| const double P_amp = hbar*omega / fEpsilon; | ||
|
|
||
| return P_cav + P_loss + P_isol + P_amp; | ||
| } | ||
|
|
||
| } /* namespace Katydid */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename this to
KatydidPSNoiseCavityConfig.yamlto stick with CamelCase and have the "NoiseCavity" modifier before "Config"