# Smart-Median: A New Real-Time Algorithm for Smoothing Singing Pitch Contours
## 4 Smart-Median: A Real-Time Pitch Contour Smoother Algorithm
### Smart-Median Algorithm  
The flowchart shown in Figure 3 illustrates how incorrectly estimated pitches can be distinguished. In addition, it indicates which estimated pitches should be selected to calculate the median for the wrongly detected pitches. There are several variables and functions in the flowchart, explained as follows:  
Figure 3: The central part of the Smart-Median algorithm for smoothing a pitch contour. 1. \(F_{i}\) refers to the frequency at index \(i\). 2. AFD (Acceptable Frequency Difference) indicates the maximum pitch frequency interval acceptable for jumping between two consequent detected pitches. In two studies on speech contour-smoother algorithms [2; 20], 30 Hz was selected as the AFD according to the researchers' experiences. Because the frequency range that humans use for singing is wider than for speaking, a larger AFD is needed for singing. According to the dataset used, the largest interval between two consequently notes sung by men was from C4 to F4, at frequencies of approximately 261 Hz and 349 Hz, respectively, so the maximum interval was 88 Hz for men. The largest interval between notes sung by women was C5 to F5, at frequencies of approximately 523 Hz and 698 Hz, respectively. Therefore, the biggest interval for women was 175 Hz. According to our observations of pitch contours, the human voice cannot physically produce such a big jump within a 30 ms timestep; i.e., for moving from C4 to F4 or from C5 to F5, more than 30 ms is needed. Therefore, it was found that an AFD with a value of 75 Hz was an acceptable choice for pitch contours comprised mostly of frequencies less than 300 Hz (male voices). For those with frequencies that mostly greater than 300 Hz (female singers), 110 Hz was a good choice of AFD. 3. noZero: this is the minimum number of consequent zero pitch frequencies that should be considered a correctly estimated silence or rest. In this study, 50 milliseconds was regarded as the minimum duration for silence to be accepted as correct [43]; otherwise, the silence requires adjustment to the local median value. 4. The _ZeroCounter(i)_ method calculates how many frequencies (pitches) of zero value exist after index \(i\). The reason for checking the number of zero values (silence) is to ascertain whether or not the pitch detector algorithm has estimated a region of silence correctly or in error. 5. _Median(i,j)_: calculates the median based on pitch frequencies from index \(i\) to index \(j\). 6. PD (Prior Distance): this indicates how many estimated pitches before the current pitch frequency should be considered for the median. In this study, the PD was calculated to cover three estimated pitch frequencies, approximately 35 and 70 milliseconds for men's and women's voices, respectively. Nevertheless, the algorithm does not need to wait until this duration becomes available, e.g., at a time of 20 milliseconds, covering 20 milliseconds with PD is sufficient. 7. FD (Following Distance): indicates how many estimated pitches after the current pitch frequency should be considered for the median. In this study, the number three was assigned to FD, meaning that to calculate the median of the current wrongly estimated pitch required 35 milliseconds for women's voices and 70 milliseconds for men's voices. Therefore, in real-time environments a delay is required until three more estimated pitches are available. 8. MaxF0: indicates the maximum acceptable frequency. In this study, for male voices, a value of 600 Hz (near to tenor) and for female voices, a maximum of 1050 Hz (soprano) were considered for MaxF0. Rarely, male and female voices may exceed these boundaries. However, if the singer's voice range is higher than these boundaries, a higher value can be considered for MaxF0. The first condition in Figure 3 aims to calculate whether the frequency at index \(i\) is valid. There are three conditions for considering invalid estimates of pitch frequency.