astrolab.timing module
This package contains a list of functions that can be used to analyse signals for the Doppler Effect experiment.
- astrolab.timing.load_signal(filename, clip=None, print_log=False, color='steelblue', fig=None, ax=None)[source]
Load a
.wavfile and return both the time-series information and the sample rate.Parameters
- filename: str
The
.wavfile to read: a filename pointing to the location of the file.- clip: array_like, default: None
A two-element list
[a,b]and clips the input signal between the timesaandb(in seconds).This can be useful if you want to remove the first
aseconds or the lastbseconds of your sound file. By default, no clipping is done.- print_log: bool, default: False
A boolean variable to decide whether you want to print a log of what you’ve done. In this case, whether you want to plot the resulting signal or not. By default, the signal is not plotted.
- color: str, default: “steelblue”
Colour of this plot. Must be one of the matplotlib “named colors”: https://matplotlib.org/stable/gallery/color/named_colors.html.
- fig: matplotlib figure object, default: None
Figure on which to plot the result. By default, a new figure is created.
- ax: matplotlib axes object, default: None
Axes on which to plot the result. By default, a new axis is created.
Returns
- samplerate: int
The sample rate of the signal. This is defined as the number of datapoints of the input signal per second. Typically, most recordings are taken at 44,100 Hz, meaning that there are 44,100 samples in each second.
- times: array_like
An array of the time-steps at which the data is sampled, in seconds.
- signal: array_like
An array containing the value of the amplitude of the signal at each time-step in times.
Raises
- ValueError
In case the
cliparray’s entries are not in ascending order.
Usage
>>> sr, times, sig = load_signal("./filename.wav", clip=[3,20], print_log=True, color='firebrick', fig=None, ax=None)
- astrolab.timing.chunk_signal(times, signal, size=2048, step=128)[source]
Break up a signal (given in the
signalarray) into individual pieces (or chunks) of sizesize, whose left-edges are separated by astepsamples.Thus, if “chunk 0” ranges from
(0, size), “chunk 1” would range from(step, size + step). Notice how both chunks aresizesamples wide, and their left edges are separated bystepsamples. The splitting is thus done with significant overlap.For each of these chunks, it returns the average time at which the chunk was taken, and the signal’s amplitude values within that chunk.
Parameters
- times: array_like
An array of times, usually one of the outputs of the
load_signalfunction.- signal: array_like
An array of amplitudes for each of the times in
times, also typically one of the outputs of theload_signalfunction.- size: int, default: 2048
The number of samples in a single chunk.
- step: int, default: 128
The number of samples between the left-edges of successive chunks.
Returns
- t: array_like
A one-dimensional array containing the average time values for each of the chunks.
- chunks: array_like
A two-dimensional array. The first dimension is the chunk-number. For each chunk, it returns size number of amplitude values.
Usage
>>> average_times, chunks = chunk signal(times, sig)
- astrolab.timing.power_spectrum(chunk, samplerate, lowx=1000, highx=10000, print_log=False, color='coral', alpha=1, label=None, fig=None, ax=None)[source]
Accept an array of signal values, compute its power spectrum, and return the dominant frequency in the range
[lowx, highx]. Ideally, this range should be centered around the expected “stationary” frequency, and should be wide enough contain all the variation in the frequency during the object’s motion.Parameters
- chunk: array_like
An array of amplitude values, usually a specific chunk that was returned by the
chunk_signalfunction, although the entire signal could be used as well.- samplerate: int
The original samplerate of the signal, obtained from the
load_signalfunction.- lowx: float, default: 1000
Lower limit (in Hz) of the frequency-range within which the dominant frequency is found and (if
print_log=True) the result is plotted.- highx: float, default: 10_000
Upper limit (in Hz) of the frequency-range within which the dominant frequency is found and (if
print_log=True) the result is plotted.- print_log: bool, default: False
A boolean variable to decide whether you want to print a log of what you’ve done. In this case, if
print_log=True, the resulting power spectrum is plotted. By default, the power spectrum is not plotted.- color: str, default: “coral”
Set the colour of the plot that is produced if
print_log=True.- alpha: float, default: 1
Set the transparency of the plot that is produced if
print_log=True.- label: str or None, default: None
Set the label of the plot that is produced if
print_log=True. By default, no label is set.- fig: matplotlib figure object, default: None
Figure on which to plot the result. By default, a new figure is created.
- ax: matplotlib axes object, default: None
Axes on which to plot the result. By default, a new axis is created.
Returns
- dom_freq: float
The frequency in the range
[lowx , highx]that contributes the greatest deal to our signal in the given range of frequencies.
Usage:
>>> dominant_frequency = power_spectrum(chunks[0], samplerate, lowx=2000, highx=4000)