Skip to content

Downsampling using Sinc causes aliasing #174

Description

@Azorlogh

Thanks to @ollpu for figuring this out

Here is a minimal example:
It generates a 900hz sine wave at a 2000hz sample rate, which is then downsampled to 1500hz. The Nyquist frequency of the new signal is 750hz, so we hear an alias of our original signal at 600hz. If I understand correctly, the sinc should have the cutoff set to filter out everything above 750hz in our original signal to avoid this.

use dasp::{interpolate::sinc::Sinc, ring_buffer, signal, Sample, Signal};
use hound::{WavSpec, WavWriter};

fn main() {
    let my_signal = signal::rate(2000.0).const_hz(900.0).sine();

    let mut spec = WavSpec {
        channels: 1,
        sample_rate: 2000,
        bits_per_sample: 16,
        sample_format: hound::SampleFormat::Int,
    };

    let ring_buffer = ring_buffer::Fixed::from([0.0; 100]);
    let sinc = Sinc::new(ring_buffer);
    let my_signal = my_signal.from_hz_to_hz(sinc, 2000.0, 1500.0);
    spec.sample_rate = 1500;

    let mut writer = WavWriter::create("my_output.wav", spec).unwrap();

    for frame in my_signal.take(2000 * 4) {
        writer
            .write_sample((frame * i16::MAX as f64) as i16)
            .unwrap();
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions