levisim.analysis.spectral_density_estimate¶
Spectral density estimates (PSDs and CSDs) of recorded time traces, by Bartlett’s method
- class levisim.analysis.spectral_density_estimate.SpectralDensityEstimate(source, AB, num_segments=None, num_samples_per_segment=None, desired_Δf=None, one_sided=True, **kwargs)[source]¶
Bases:
objectEstimates S_AB, i.e. the spectral density:
PSD (Power Spectral Density), if B=A, e.g. Sxx, Syy etc.
CSD (Cross-correlation Spectral Density), if B≠A, e.g. Sxy etc.
Bartlett’s method: the traces are cut into num_segments non-overlapping segments, each is (optionally) windowed and Fourier transformed, and the periodograms X*_i(f) Y_i(f) are averaged. Normalised so that, for the default rectangular window, the two-sided estimate obeys Parseval exactly: Σ S(f_k) Δf = 〈a b〉 over the averaged samples. Units: [A][B]/Hz, e.g. m²/Hz.
Any trace in source.traces can be used, e.g. ‘x’, ‘y’, ‘z’, the momenta ‘px’, ‘py’, ‘pz’ (Simulation(store_momenta=True)) or the radial ‘r’ = √(x² + y²) (Simulation(store_radial=True)); pass multi-character axes as a tuple, e.g. (‘px’, ‘px’). A PSD of ‘x’, ‘y’ or ‘z’ is fitted automatically (fit_PSD) when the source has a Trap. NOTE: momentum spectra follow from the position ones, Spp(ω) = m²ω² Sxx(ω) (since p = m dx/dt); the radial trace r(t) is a non-linear function of x, y, so its spectrum mixes them (difference and sum frequencies Ωx ± Ωy, and near-DC power from 〈r〉 fluctuations) and has no single Lorentzian form.
Keyword arguments: window [None => rectangular], see the window property; fit_PSD_kwargs; num_segments_to_average; debug.
- properties = ['Axes', 'Type', 'One Sided', 'Num. Segments', 'Num. Segments to Average', 'Segment Width (Samples/Segment)', 'Length of Freq. Range (# of Freq. bins)', 'Segment Time, 1/Δf (ms)', 'Frequency Step, Δf (Hz)', 'Averaging time (ms)', 'Measurement bandwidth (Hz)', 'Ω (kHz)', 'Temperature, via Area (K)', 'Variance, σ² via Area (nm²)', 'St.Dev., σ, via Area (nm)', 'Window', 'simulation', 'id(simulation)', 'self', 'id(self)']¶
- static make_window(window, num_samples)[source]¶
Segment window, normalised to unit mean square so that broadband PSD levels are preserved window: None or ‘rectangular’ => 1 (no window); a scipy.signal.get_window spec, e.g. ‘hann’, ‘blackmanharris’, (‘tukey’, 0.25); or an array of length num_samples [·]
- property spectrum¶
[[f, S(f)], …] (complex dtype, since a CSD is complex) [Hz, units²/Hz]
- Type:
The frequencies and values as one 2D array, shape (len(freq_range), 2)
- property info¶
Prints info on the spectral density estimate
- define_averaging_variables(num_segments, num_samples_per_segment, desired_Δf)[source]¶
Given one of the three:
- num_segments (to divide the time trace into) or - num_samples_per_segment (how many time step samples of the time trace do we want to have per segment?) or - desired_Δf (how big a frequency bin size do we want?)
the other two can be derived. (NOTE: I do not do any zero-padding of the data!)
In particular, the frequency bin size can be seen as:
Δf = frequency step size for each segmented PSD estimate Δf = 1 / segment_time equivalent to Δf = num_segments / time_length Δf = sample_rate / num_samples_per_segment (where sample_rate = 1 / Δt) Δf = the index-to-frequency(Hz) conversion factor, in Hertz
Also, in particular:
segment_time = time length of each segment, equivalent to: segment_time = 1 / Δf segment_time = num_samples_per_segment * Δt segment_time = time_length / num_segments segment_time = the frequency(Hz)-to-index conversion factor, in seconds
- property segment_time¶
Time length of a segment of the time trace that a one-shot spectral density estimate is made from
- property Ω¶
Estimates the resonant frequency (rad/s) by finding the numerical peak of the spectrum
- property Γ¶
Estimates the full-width half maximum (rad/s) by calculating the difference between the two values closest to half the spectral peak value
- property area¶
Area under the curve of the values across the frequency range (trapezoidal rule) NOTE: np.trapz was removed in NumPy 2.4, which made this (and temperature_via_area) raise; scipy.integrate.trapezoid is the same algorithm. [units of the trace]²
- property temperature_via_area¶
Bendat, Piersol – Random Data - Analysis and Measurement Procedures, p147, Eq 5.125
Area under PSD = Var(x) =〈x²〉 〈x²〉= (1/2π) ∫ Sxx(Ω)dΩ 〈x²〉= ∫ Sxx(f)df = ∫ Gxx(f)df For two-sided Sxx, the limits are -∞ to +∞ For one-sided Gxx = 2 * Sxx, the limits are 0 to +∞
Equipartition theorem
½mΩ²〈x²〉 = ½kb*T
Therefore expecting T = (mΩ² / kb) * Area
NOTE: since 〈z〉≠ 0, Var(z) =〈z²〉- 〈z〉², so the z-trace needs to be adjusted for it to give a sensible answer for the temperature_via_area
- Type:
NOTE
- frequency_to_index(lower_bound_Hz, upper_bound_Hz)[source]¶
Converts a lower and upper frequency bound (in Hz) into the indices of the arrays of the spectral estimate
- get_plot_points(lower_bound_Hz, upper_bound_Hz)[source]¶
Returns the portions of the arrays given by the lower and upper frequency bounds
- fit_PSD(A0=None, Γ0=None, Ω0=None, fit_range=(1000, 500000), ftol=1e-10, maxfev=1000000000.0, debug=False)[source]¶
Fits for the PSDs, all done in Hertz
- property temperature_via_fit¶
Effective temperature from the fitted PSD amplitude
Afit = 2 kB T_eff / m (two-sided), 4 kB T_eff / m (one-sided) [K]