# Constant-Q transform

In mathematics and signal processing, the constant-Q transform, simply known as CQT transforms a data series to the frequency domain. It is related to the Fourier transform and very closely related to the complex Morlet wavelet transform. Its design is suited for musical representation. Constant-Q transform applied to the waveform of a C major piano chord. The x-axis is frequency, mapped to standard musical pitches, from low (left) to high (right). The y-axis is time, starting from pressing the piano chord at the bottom, and releasing the piano chord at the top, 8 seconds later. Darker pixels correspond to higher values of the Constant-Q transform. The peaks correspond closely to the precise frequencies of the vibrating piano strings. Thus the peaks can be used to detect the notes played on the piano. The lowest 3 peaks are the fundamental frequencies of the C major chord (C, E, G). Each string also vibrates at multiples of the fundamental, known as overtones, which correspond to the remaining smaller peaks to the right of the fundamental pitches. The overtones are smaller in intensity than the fundamental pitch.
Audio of the C Major piano chord used to generate the Constant-Q transform above.

The transform can be thought of as a series of filters fk, logarithmically spaced in frequency, with the k-th filter having a spectral width δfk equal to a multiple of the previous filter's width:

$\delta f_{k}=2^{1/n}\cdot \delta f_{k-1}=\left(2^{1/n}\right)^{k}\cdot \delta f_{\text{min}},$ where δfk is the bandwidth of the k-th filter, fmin is the central frequency of the lowest filter, and n is the number of filters per octave.

## Calculation

The short-time Fourier transform of x[n] for a frame shifted to sample m is calculated as follows:

$X[k,m]=\sum _{n=0}^{N-1}W[n-m]x[n]e^{-j2\pi kn/N}.$ Given a data series at sampling frequency fs = 1/T, T being the sampling period of our data, for each frequency bin we can define the following:

• Filter width, δfk.
• Q, the "quality factor":
$Q={\frac {f_{k}}{\delta f_{k}}}.$ This is shown below to be the integer number of cycles processed at a center frequency fk. As such, this somewhat defines the time complexity of the transform.
• Window length for the k-th bin:
$N[k]={\frac {f_{\text{s}}}{\delta f_{k}}}={\frac {f_{\text{s}}}{f_{k}}}Q.$ Since fs/fk is the number of samples processed per cycle at frequency fk, Q is the number of integer cycles processed at this central frequency.

The equivalent transform kernel can be found by using the following substitutions:

• The window length of each bin is now a function of the bin number:
$N=N[k]=Q{\frac {f_{\text{s}}}{f_{k}}}.$ • The relative power of each bin will decrease at higher frequencies, as these sum over fewer terms. To compensate for this, we normalize by N[k].
• Any windowing function will be a function of window length, and likewise a function of window number. For example, the equivalent Hamming window would be
$W[k,n]=\alpha -(1-\alpha )\cos {\frac {2\pi n}{N[k]-1}},\quad \alpha =25/46,\quad 0\leqslant n\leqslant N[k]-1.$ • Our digital frequency, ${\frac {2\pi k}{N}}$ , becomes ${\frac {2\pi Q}{N[k]}}$ .

After these modifications, we are left with

$X[k]={\frac {1}{N[k]}}\sum _{n=0}^{N[k]-1}W[k,n]x[n]e^{\frac {-j2\pi Qn}{N[k]}}.$ ## Fast calculation

The direct calculation of the constant-Q transform is slow when compared against the fast Fourier transform (FFT). However, the FFT can itself be employed, in conjunction with the use of a kernel, to perform the equivalent calculation but much faster. An approximate inverse to such an implementation was proposed in 2006; it works by going back to the DFT, and is only suitable for pitch instruments.

A development on this method with improved invertibility involves performing CQT (via FFT) octave-by-octave, using lowpass filtered and downsampled results for consecutively lower pitches. Implementations of this method include the MATLAB implementation and LibROSA's Python implementation. LibROSA combines the subsampled method with the direct FFT method (which it dubs "pseudo-CQT") by having the latter process higher frequencies as a whole.

The sliding DFT can be used for faster calculation of constant-Q transform, since the sliding DFT does not have to be linear-frequency spacing and same window size per bin.

## Comparison with the Fourier transform

In general, the transform is well suited to musical data, and this can be seen in some of its advantages compared to the fast Fourier transform. As the output of the transform is effectively amplitude/phase against log frequency, fewer frequency bins are required to cover a given range effectively, and this proves useful where frequencies span several octaves. As the range of human hearing covers approximately ten octaves from 20 Hz to around 20 kHz, this reduction in output data is significant.

The transform exhibits a reduction in frequency resolution with higher frequency bins, which is desirable for auditory applications. The transform mirrors the human auditory system, whereby at lower-frequencies spectral resolution is better, whereas temporal resolution improves at higher frequencies. At the bottom of the piano scale (about 30 Hz), a difference of 1 semitone is a difference of approximately 1.5 Hz, whereas at the top of the musical scale (about 5 kHz), a difference of 1 semitone is a difference of approximately 200 Hz. So for musical data the exponential frequency resolution of constant-Q transform is ideal.

In addition, the harmonics of musical notes form a pattern characteristic of the timbre of the instrument in this transform. Assuming the same relative strengths of each harmonic, as the fundamental frequency changes, the relative position of these harmonics remains constant. This can make identification of instruments much easier. The constant Q transform can also be used for automatic recognition of musical keys based on accumulated chroma content.

Relative to the Fourier transform, implementation of this transform is more tricky. This is due to the varying number of samples used in the calculation of each frequency bin, which also affects the length of any windowing function implemented.

Also note that because the frequency scale is logarithmic, there is no true zero-frequency / DC term present, which may be a drawback in applications that are interested in the DC term. Although for applications that are not interested in the DC such as audio, this is not a drawback.