Author
Affiliation

Cam

Anauseam

Published

2026-03-31

Digital Signal Processing

Now that we have digitized the signal into the discrete sequence x[n], we can begin developing an algorithm to analyze and process it.

To adjust the pitch of a note, we obviously need to know the frequency of the note. We will also capture information about the note’s harmonics for the purpose of inharmonicity analysis.

Inharmonicity analysis is will be done at a later state.

Fundamental Frequency Detection

There are an incredbile number of ways to determine the fundamental frequency of a signa, whether it be through traditional time-domain methods or frequency-domain methods, or modern apporaches like subspace methods and deep learning.

For simplicity and speed, we will use the YIN algorithm (Cheveigné and Kawahara 2002) for determining the fundamental frequency(f_0), which is a specialized autocorrelation-based estimator designed to minimize error rates in monophonic pitch tracking.

Cheveigné, Alain de, and Hideki Kawahara. 2002. “YIN, a Fundamental Frequency Estimator for Speech and Music.” The Journal of the Acoustical Society of America 111 (4): 1917–30. https://doi.org/10.1121/1.1458024.

The Squared Difference Function (SDF)

Standard autocorrelation maximizes the product of the signal and its time-shifted version. However, YIN minimizes the difference. We define the Squared Difference Function, d_t(\tau), for a lag \tau:

d_t(\tau) = \sum_{j=1}^{W} (x[j] - x[j + \tau])^2

Where W is the integration window size. Unlike standard autocorrelation, the SDF is sensitive to amplitude variations; however, it provides a more robust initial metric for periodicity, aiming for d_t(\tau) = 0 at the true period.

2. The Cumulative Mean Normalized Difference Function (CMNDF)

The raw SDF is prone to selecting the zero-lag (\tau=0) as the period, as d_t(0) is always 0. To mitigate this and normalize the “dip” detection, YIN introduces the CMNDF, denoted as d'_t(\tau):

d'_t(\tau) = \begin{cases} 1 & \text{if } \tau = 0 \\ \frac{d_t(\tau)}{\frac{1}{\tau} \sum_{j=1}^{\tau} d_t(j)} & \text{otherwise} \end{cases}

This normalization starts the function at 1 and forces the dip at the true period to be low (close to 0), while suppressing dips at sub-harmonics (octave errors).

Absolute Thresholding

We search for the smallest lag \tau (within a valid range for piano notes, e.g., 20\text{Hz} - 4200\text{Hz}) such that d'_t(\tau) falls below a set threshold (typically 0.1). This selects the first major dip—the fundamental—rather than the global minimum, which might be an octave error in signals with strong partials.

Parabolic Interpolation

The lag \tau is an integer index. To achieve the sub-Hertz precision required for piano tuning (often < 1 cent), we must resolve \tau to a fractional value. We fit a parabola through d'_t(\tau) and its immediate neighbors d'_t(\tau-1) and d'_t(\tau+1). The minimum of this parabola yields the fractional period \tau_{frac}, providing the estimated fundamental frequency:

f_0 = \frac{f_s}{\tau_{frac}}

Spectral Analysis

Once f_0 is identified via YIN, we must analyze the spectral content to measure the inharmonicity of the piano string. Real piano strings are not ideal oscillators; their stiffness causes restoring forces that sharpen higher harmonics.

The n-th partial frequency, f_n, deviates from the ideal integer multiple n f_0 according to the inharmonicity coefficient B:

f_n = n f_0 \sqrt{1 + B n^2}

To extract these partials accurately, we perform spectral analysis guided by our initial f_0 estimate.

Windowing and the DFT

We transform the time-domain signal x[n] to the frequency domain X[k] using the Discrete Fourier Transform (DFT), usually implemented via FFT:

X[k] = \sum_{n=0}^{N-1} x[n] w[n] e^{-j 2\pi k n / N}

Windowing (w[n]): Direct application of the FFT on a truncated signal is equivalent to a rectangular window, which convolves the spectrum with a \text{sinc} function. The high side-lobes of the \text{sinc} function can cause spectral leakage, where the energy of a loud fundamental obscures the quieter high-frequency partials we need to measure.

For piano analysis, we utilize a Blackman-Harris window. It possesses a wide main lobe but extremely aggressive side-lobe attenuation (>92 \text{ dB}), ensuring that leakage from the fundamental does not corrupt the measurement of the 2nd, 3rd, or 4th partials.

Harmonic Tracking and Peak Interpolation

Using the estimated f_0 from YIN, we set search regions in the FFT spectrum centered around the theoretical locations of harmonics (2f_0, 3f_0, \dots).

Since the FFT bins are discrete (spaced by \Delta f = f_s / N), the true peak of a partial will likely fall between bins. We apply Gaussian or Parabolic interpolation on the magnitude spectrum |X[k]| around the local maximum to refine the frequency estimate of each partial.

The deviation of these measured partials from the ideal harmonic series constitutes the “Railsback Curve” data, which dictates the required stretch tuning for the specific instrument.