compute the short-time Fourier transform of a signal
ShortTimeFourierTransform( signal, options )
1-D rtable or list of data.
samplerate: (optional) Positive numeric value for the sampling rate. The default is 1.0.
overlapsize: (optional) Non-negative integer which specifies the target minimum overlap size of the segments. The default is 0.
segmentsize: (optional) Positive integer for the size of the overlapping segments. The default is the largest power of 2 that is not larger than the size of signal.
fftnormalization: (optional) One of none, symmetric, or full, indicates the normalization to be applied when using the Fast Fourier Transform (FFT). The default is symmetric.
temperendpoints: (optional) Either true or false, specifies whether the short-time power spectra are to be tempered at the endpoints. The default is false.
window: (optional) Either a list, name, or string, specifies the windowing command to be applied to the overlapping segments. The default is "none" (for no windowing to be applied). If a list is passed, the first element provides the name of the windowing command, and any remaining terms are passed as options to the command.
windownormalization (optional) Either true or false, indicates if the windowing function is to be normalized. The default is true.
frequencyunit: (optional) Unit which specifies the unit of frequency. The default is Unit(Hz). Either of the forms algebraic or Unit(algebraic) is accepted, and the unit must be convertible to a valid unit of frequency.
timeunit: (optional) Unit which specifies the unit of time. The default is Unit(s). Either of the forms algebraic or Unit(algebraic) is accepted, and the unit must be convertible to a valid unit of time.
powerscale: (optional) Unit which indicates the scaling, if any, to be applied to the power spectrum. Either of the forms algebraic or Unit(algebraic) is accepted, and the unit must be convertible to a valid unit of power (see below for more details). The default is Unit(1/Hz).
downsample: (optional) Positive integer, which specifies the down-sample factor applied to the spectrogram. The default is 1.
spectrogramoptions: (optional) List of additional plot options to be passed when creating the spectrogram. The default is .
output: (optional) The type of output. The supported options are:
stft: Returns a Matrix of float datatype containing the Short-Time Fourier Transform (STFT). This is the default.
stps: Returns a Matrix of float datatype containing the Short-Time Power Spectrum (STPS).
signal: Returns a Matrix of float or complex datatype containing the signal, with each column representing a short-time segment.
frequencies: Returns a Vector, of float datatype and length the same as signal, containing the frequencies.
times: Returns a Vector, of float datatype and length the same as signal, containing the times.
spectrogram: Returns the spectrogram of the STPS.
record: Returns a record with the previous options.
list of any of the above options: Returns an expression sequence with the corresponding outputs, in the same order.
The ShortTimeFourierTransform command takes a 1-D rtable or list signal, and computes the Short-Time Fourier Transform using the provided options.
The STFT is calculated using the following steps:
Divide the signal into segments of equal size with equal or nearly equal overlaps.
Apply the windowing procedure to each segment.
Take the Discrete Fourier Transform (DFT) of each windowed segment.
Form the STFT Matrix, with each column corresponding to a segment.
The values of a=overlapsize, b=segmentsize, and n=numelems⁡signal must satisfy 2≤n, 0≤a, 2≤b, a<b, and b≤n.
Since the DFT will be computed for each segment, it is suggested that, for larger signal lengths, segmentsize be a power of 2 and no less than 4, so that the FFT will be utilized.
The passed value of overlapsize is used to determine the number c of overlapping segments of size b=segmentsize. The values of b and c determine the smallest possible overlap size p and the excess number q of overlaps of size p+1. If we denote n=numelems⁡signal, then bc=n+p⁢c−1−q+p+1⁢q for the 1<c case. When c=1, there are no overlaps and the single segment is just the original signal.
The value of window, when not passed as a list, should be the name or string, with or without the Window suffix, that corresponds to the windowing command. For example, to use a Hamming window, you can pass window=Hamming or window="HammingWindow". In both cases, the command SignalProcessing[HammingWindow] will be used internally. Similarly, you can pass window=["Exponential",0.5] or window=[ExponentialWindow,0.5] to use SignalProcessing[ExponentialWindow] with parameter value 0.5.
To apply a window to a Vector V of length n, the window is first applied to another Vector W of size n and filled with ones, and then V is multiplied element-wise by W. When windownormalization=true, W is first normalized with respect to its Root Mean Square (RMS).
The STPS is calculated from the STFT by taking the square of the absolute value of each element. To scale the STPS with the powerscale option, units which are dimensionally equivalent to the following are accepted:
1: No further scaling is performed.
1/Hz: The STPS is divided by r=samplerate.
1/rad/Hz: STPS is divided by 2⁢π⁢r.
dB: Each element u of STPS is replaced with 10⁢Typesetting:-_Hold⁡%log10⁡u.
dB/Hz: Each element u of STPS is replaced with 10⁢Typesetting:-_Hold⁡%log10⁡ur.
dB/rad/Hz: Each element u of STPS is replaced with 10⁢Typesetting:-_Hold⁡%log10⁡u2⁢π⁢r.
When temperendpoints=true, the values in the first and last rows of the unscaled STPS Matrix are halved. Note that the segment size must be three or more for tempering.
The frequencies and times Vectors are of size n=numelems⁡signal, and have components defined by, respectively, Fi=i−1⁢rn and Ti=i−1r, where r=samplerate.
The samplerate option can also include a unit of frequency. If a unit is provided, and it differs from frequencyunit, then the sample rate will be converted to use the same unit as frequencyunit.
If signal is an rtable of type AudioTools:-Audio, the sample rate is inferred from the attributes. Should samplerate also be passed, it will be overridden.
When the signal is real-valued and thus the STPS is symmetric, the spectrogram will display only the first half of frequency components, and the remaining STPS will be doubled in value.
Maple will attempt to coerce the provided signal to a 1-D Vector of either float or complex datatype, and an error will be thrown if this is not possible. For this reason, it is most efficient for the passed input to use this datatype.
The input signal cannot have an indexing function, and must use rectangular storage.
The ShortTimeFourierTransform command is not thread safe.
Here, we will create a signal, and then find the STFT along with the spectrogram. First, create Vectors for the times and signal:
T ≔ Vector⁡seq⁡0..599,datatype=float8
g ≔ t→2⁢cos⁡Pi⁢t5+9⁢sin⁡3⁢Pi⁢t4
X ≔ mapevalhf⁡g,T
Second, define a sampling rate:
r ≔ 1.0
Third, we choose overlap and segment sizes:
a ≔ 50
b ≔ 64
Finally, we find the STFT and spectrogram, which can be exported in a record, to prevent re-calculation:
R ≔ ShortTimeFourierTransform⁡X,samplerate=r,overlapsize=a,segmentsize=b,powerscale=dBHz,output=record:
The signal can also originate from a WAV file recording of a violin:
file ≔ cat⁡kernelopts⁡datadir,kernelopts⁡dirsep,audio,kernelopts⁡dirsep,ViolinThreePosVibrato.wav
Violin ≔ ToMono⁡Read⁡file,samples=1000..5000
Violin≔Sample Rate44100File FormatPCM File Bit Depth16Channels1Samples/Channel4001Duration0.09073⁢s
a ≔ 128
b ≔ 256
The spectrogram can be generated with less points by using down sampling. For instance, the following reduces the number of elements in each column of the STPS by a factor of 4:
The SignalProcessing[ShortTimeFourierTransform] command was introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
Download Help Document