Skip to content

Conversation

@winkt0
Copy link
Contributor

@winkt0 winkt0 commented Oct 28, 2025

YIN algorithm

Description

Implementation of the YIN algorithm for estimating the fundamental frequency of an audio signal. With optional parabolic interpretation for refining the result. I added the following files:

signal_analysis/yin.rs
signal_analysis/mod.rs

I added a new folder signal_analysis because I felt the algorithm did not feat neatly into the other categories. Let me know if that's okay or I should put it somewhere else.

I added #[allow(clippy::needless_range_loop)] on the function difference_function_values because I felt that clippy's suggestion to use an iterator and enumerate decreases the readability of the function (especially for people new to Rust) for no substantial performance improvement.

The YIN algorithm is based on: De Cheveigné, A., & Kawahara, H. (2002). YIN, a fundamental frequency estimator for speech and music. The Journal of the Acoustical Society of America, 111(4), 1917–1930. https://doi.org/10.1121/1.1458024

Closes #939

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@winkt0 winkt0 requested a review from imp2002 as a code owner October 28, 2025 18:23
@winkt0
Copy link
Contributor Author

winkt0 commented Nov 2, 2025

Hi @siriak, can you check this pr when you get a chance? Thanks in advance!

@siriak siriak requested a review from Copilot November 2, 2025 12:11
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the YIN algorithm for pitch detection in audio signals, adding a new signal_analysis module to the codebase. The YIN algorithm is a well-known pitch estimation technique that uses autocorrelation-based methods to detect fundamental frequencies in audio signals.

  • Implements the complete YIN algorithm including difference function, cumulative mean normalized difference function, and parabolic interpolation for improved accuracy
  • Adds comprehensive test coverage with various sine wave scenarios including harmonic and inharmonic combinations
  • Integrates the new module into the project structure with proper exports and documentation

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/signal_analysis/yin.rs Implements the YIN pitch detection algorithm with Yin struct, YinResult struct, and supporting functions for signal analysis
src/signal_analysis/mod.rs Module file that exports the Yin and YinResult types
src/lib.rs Adds the signal_analysis module to the public API
DIRECTORY.md Updates documentation to include the new YIN implementation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link

codecov-commenter commented Nov 2, 2025

Codecov Report

❌ Patch coverage is 99.57447% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.44%. Comparing base (db3f011) to head (a30b9d7).

Files with missing lines Patch % Lines
src/signal_analysis/yin.rs 99.57% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #940      +/-   ##
==========================================
+ Coverage   95.39%   95.44%   +0.04%     
==========================================
  Files         331      332       +1     
  Lines       21261    21496     +235     
==========================================
+ Hits        20282    20516     +234     
- Misses        979      980       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@siriak
Copy link
Member

siriak commented Nov 2, 2025

Please check comments

…Result. This is necessary to communicate clearly that yin can fail if no argmin for CMNDF is found, for which get_frequency would return inf because the best lag would be 0. Added a test for this case.
@winkt0
Copy link
Contributor Author

winkt0 commented Nov 2, 2025

I adjusted the relevant code as suggested

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 7c1542b into TheAlgorithms:master Nov 2, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YIN algorithm

3 participants