Analysis
compute_fft(x_data, y_data)
Computes the Fast Fourier Transform (FFT) of a signal and returns the positive frequency spectrum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_data
|
ndarray
|
Time or independent variable array, assumed to be uniformly spaced. |
required |
y_data
|
ndarray
|
Signal data corresponding to |
required |
Returns:
Name | Type | Description |
---|---|---|
positive_freqs |
ndarray
|
Array of positive frequency components corresponding to the FFT. |
fft_magnitude |
ndarray
|
Magnitude of the FFT at the positive frequencies. |
Notes
- The signal is centered by subtracting its mean before computing the FFT, which removes the DC component.
- Only the positive frequency half of the FFT spectrum is returned, assuming a real-valued input signal.
- If
y_data
is complex, returned FFT values still reflect magnitude only. - The input
x_data
must be uniformly spaced for the frequency axis to be accurate.
Examples:
>>> t = np.linspace(0, 1, 1000)
>>> y = np.sin(2 * np.pi * 50 * t)
>>> freqs, spectrum = compute_fft(t, y)
Source code in sqil_core/utils/_analysis.py
compute_snr_peaked(x_data, y_data, x0, fwhm, noise_region_factor=2.5, min_points=20)
Computes the Signal-to-Noise Ratio (SNR) for a peaked function (e.g., Lorentzian, Gaussian) based on the provided fit parameters. The SNR is calculated by comparing the signal strength at the peak (x0) with the noise level estimated from a region outside the peak.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_data
|
ndarray
|
Array of x values (independent variable), typically representing frequency or position. |
required |
y_data
|
ndarray
|
Array of y values (dependent variable), representing the measured values (e.g., intensity, amplitude). |
required |
x0
|
float
|
The location of the peak (center of the distribution), often the resonance frequency or peak position. |
required |
fwhm
|
float
|
The Full Width at Half Maximum (FWHM) of the peak. This defines the width of the peak and helps determine the region for noise estimation. |
required |
noise_region_factor
|
float
|
The factor used to define the width of the noise region as a multiple of the FWHM. The noise region is
considered outside the interval |
2.5
|
min_points
|
int
|
The minimum number of data points required in the noise region to estimate the noise level. If the number of points in the noise region is smaller than this threshold, a warning is issued. |
20
|
Returns:
Type | Description |
---|---|
float
|
The computed Signal-to-Noise Ratio (SNR), which is the ratio of the signal strength at |
Notes
- The function assumes that the signal has a clear peak at
x0
and that the surrounding data represents noise. - If the noise region contains fewer than
min_points
data points, a warning is raised suggesting the adjustment ofnoise_region_factor
.
Example
x_data = np.linspace(-10, 10, 1000) y_data = np.exp(-(x_data**2)) # Example Gaussian x0 = 0 fwhm = 2.0 snr = compute_snr_peaked(x_data, y_data, x0, fwhm) print(snr)
Source code in sqil_core/utils/_analysis.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
|
estimate_linear_background(x, data, points_cut=0.1, cut_from_back=False)
Estimates the linear background for a given data set by fitting a linear model to a subset of the data.
This function performs a linear regression to estimate the background (offset and slope) from the
given data by selecting a portion of the data as specified by the points_cut
parameter. The linear
fit is applied to either the first or last points_cut
fraction of the data, depending on the cut_from_back
flag. The estimated background is returned as the coefficients of the linear fit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
The independent variable data. |
required |
data
|
ndarray
|
The dependent variable data, which can be 1D or 2D (e.g., multiple measurements or data points). |
required |
points_cut
|
float
|
The fraction of the data to be considered for the linear fit. Default is 0.1 (10% of the data). |
0.1
|
cut_from_back
|
bool
|
Whether to use the last |
False
|
Returns:
Type | Description |
---|---|
list
|
The coefficients of the linear fit: a list with two elements, where the first is the offset (intercept) and the second is the slope. |
Notes
- If
data
is 2D, the fit is performed on each column of the data separately. - The function assumes that
x
anddata
have compatible shapes.
Examples:
>>> import numpy as np
>>> x = np.linspace(0, 10, 100)
>>> data = 3 * x + 2 + np.random.normal(0, 1, size=(100,))
>>> coefficients = estimate_linear_background(x, data, points_cut=0.2)
>>> print("Estimated coefficients:", coefficients)
Source code in sqil_core/utils/_analysis.py
find_closest_index(arr, target)
find_first_minima_idx(data)
Find the index of the first local minimum in a 1D array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
array - like
|
1D sequence of numerical values. |
required |
Returns:
Type | Description |
---|---|
int or None
|
Index of the first local minimum, or None if no local minimum is found. |
Notes
A local minimum is defined as a point that is smaller than its immediate neighbors.
Uses scipy.signal.argrelextrema
to detect local minima.
Examples:
Source code in sqil_core/utils/_analysis.py
get_peaks(x_data, y_data, prominence=None, sort=True)
Detects and returns peaks in a 1D signal based on prominence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_data
|
ndarray
|
1D array of x-values corresponding to |
required |
y_data
|
ndarray
|
1D array of y-values representing the signal in which to find peaks. |
required |
prominence
|
float or None
|
Minimum prominence of peaks to detect. If None, defaults to 5% of the maximum
value in |
None
|
sort
|
bool
|
If True, peaks are sorted in descending order of magnitude. Default is True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
peak_freqs |
ndarray
|
x-values at which peaks occur. |
peak_magnitudes |
ndarray
|
y-values (magnitudes) at the detected peaks. |
Notes
- Uses
scipy.signal.find_peaks
for detection.
Examples:
>>> x = np.linspace(0, 10, 1000)
>>> y = np.sin(2 * np.pi * x) + 0.1 * np.random.randn(1000)
>>> freqs, mags = get_peaks(x, np.abs(y))
>>> print(freqs[:3], mags[:3]) # Show top 3 peak locations and magnitudes
Source code in sqil_core/utils/_analysis.py
line_between_2_points(x1, y1, x2, y2)
Computes the equation of a line passing through two points.
Given two points (x1, y1) and (x2, y2), this function returns the y-intercept and slope of the line connecting them. If x1 and x2 are the same, the function returns y1 as the intercept and a slope of 0 to avoid division by zero.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
float
|
The x-coordinate of the first point. |
required |
y1
|
float
|
The y-coordinate of the first point. |
required |
x2
|
float
|
The x-coordinate of the second point. |
required |
y2
|
float
|
The y-coordinate of the second point. |
required |
Returns:
Type | Description |
---|---|
tuple[float, float]
|
A tuple containing: - The y-intercept (float), which is y1. - The slope (float) of the line passing through the points. |
Notes
- If x1 and x2 are the same, the function assumes a vertical line and returns a slope of 0.
- The returned y-intercept is based on y1 for consistency in edge cases.
Examples:
Source code in sqil_core/utils/_analysis.py
linear_interpolation(x, x1, y1, x2, y2)
Performs linear interpolation to estimate the value of y at a given x.
This function computes the interpolated y-value for a given x using two known points (x1, y1) and (x2, y2) on a straight line. It supports both scalar and array inputs for x, enabling vectorized operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float or ndarray
|
The x-coordinate(s) at which to interpolate. |
required |
x1
|
float
|
The x-coordinate of the first known point. |
required |
y1
|
float
|
The y-coordinate of the first known point. |
required |
x2
|
float
|
The x-coordinate of the second known point. |
required |
y2
|
float
|
The y-coordinate of the second known point. |
required |
Returns:
Type | Description |
---|---|
float or ndarray
|
The interpolated y-value(s) at x. |
Notes
- If x1 and x2 are the same, the function returns y1 to prevent division by zero.
- Assumes that x lies between x1 and x2 for meaningful interpolation.
Examples:
>>> linear_interpolation(3, 2, 4, 6, 8)
5.0
>>> x_vals = np.array([3, 4, 5])
>>> linear_interpolation(x_vals, 2, 4, 6, 8)
array([5., 6., 7.])
Source code in sqil_core/utils/_analysis.py
remove_linear_background(x, data, points_cut=0.1)
Removes a linear background from the input data (e.g. the phase background of a spectroscopy).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
Input data. Can be a 1D vector or a 2D matrix. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The input data with the linear background removed. The shape of the
returned array matches the input |
Source code in sqil_core/utils/_analysis.py
remove_offset(data, avg=3)
Removes the initial offset from a data matrix or vector by subtracting
the average of the first avg
points. After applying this function,
the first point of each column of the data will be shifted to (about) 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
Input data, either a 1D vector or a 2D matrix |
required |
avg
|
int
|
The number of initial points to average when calculating the offset, by default 3 |
3
|
Returns:
Type | Description |
---|---|
ndarray
|
The input data with the offset removed |
Source code in sqil_core/utils/_analysis.py
soft_normalize(data)
Apply soft normalization to a 1D or 2D array with optional NaNs.
This function performs z-score normalization followed by a smooth non-linear compression using a hyperbolic tangent (tanh) function. It is designed to reduce the effect of outliers while preserving the dynamic range of typical data values. The result is rescaled to [0, 1].
For 2D arrays, normalization is done row-wise, but compression is based on a global threshold across all non-NaN entries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
ndarray
|
Input data, must be a 1D or 2D NumPy array. Can contain NaNs. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Normalized data, same shape as input, with values scaled to [0, 1]. NaNs are preserved. |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Notes
- Z-score normalization is done using nanmean and nanstd.
- Outliers are compressed using a tanh centered at a scaled threshold.
- Output values are guaranteed to be in [0, 1] range, except NaNs.
- Rows with zero standard deviation are flattened to 0.5.