Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Audio resampling

  1. #1
    Senior Member
    Join Date
    Jul 2014

    Audio resampling

    I face the following problem
    I wanted to do some audio processing on teensy with sampling frequencies higher than 44.1 kHz.
    OK, this is easy, but I wanted to use the usb-audio to listen to the acquired sound in real time.
    First thought: modify usb-audio for other than 44.1 kHz, but this would interfere with teensyduino upgrades.
    Second thought: convert on-teensy audio to 44.1 kHz and use standard usb-audio.

    This is what I now follow
    For this I need a resampling scheme that is somewhat efficient.
    All my frequencies are above 44.1kHz so I need a decimation/interpolation scheme.

    So I played with matlab and come up with
    % original signal
    % output sampling rate
    % 'ideal' downsampling
    % sample-based resampling
    % scale both sampling frequencies to smallest integer (to be done once)
    jfs1=fs1; jfs2=fs2;
    while((mod(jfs1,1)==0) && (mod(jfs2,1)==0)), jfs1 = jfs1/10; jfs2=jfs2/10; end  
    % interpolation using integer arithmetic 
    plot(t1,s1,'.-', t2,s2,'*-',t2,s3,'ro-')
    xlim([0 0.001])
    where the interpolation is obtained by using in integer division and modulo operations and compared to Matlab's interp1 function
    The result is shown in this figure
    Click image for larger version. 

Name:	resample.jpg 
Views:	152 
Size:	18.6 KB 
ID:	10769

    I'm confident that this method can easily be implemented in 'C'.
    If this is well know method, OK, I reinvented the wheel (which is part of the fun)
    Last edited by WMXZ; 06-09-2017 at 03:18 PM.

  2. #2
    Junior Member
    Join Date
    Apr 2017

    The ARM DSP library has a very good linear interpolation function that is pretty efficient, see here
    However, linear interpolation introduces some audible artefacts.

    For higher quality and for resampling by a ratio or factor, the upsampling/decimation+ FIR method should give a much better quality at the expense of an higher computationnal cost.


    You will also find several interpolation types and examples used in my distortion modelling codes:

    For examples, look at the TD_waveshaper.cpp & .h for integer interpolation and resampling as well as TD_multistagedist.cpp & .h for floating point

  3. #3
    Senior Member
    Join Date
    Jul 2014
    Quote Originally Posted by jcugnoni View Post
    For higher quality and for resampling by a ratio or factor, the upsampling/decimation+ FIR method should give a much better quality at the expense of an higher computationnal cost.
    I have no problem replacing Newton interpolation by FIR interpolation. The computational cost is only higher.

    Note: this approach is to provide a easy method to monitor Audio processing that occur at higher sampling frequencies (192 kHz 375 kHz).
    required 22 kHz anti-aliasing is done and ultrasonic sound may be base-band shifted, etc. etc. in other parts of a functioning program.

    To address the critic on 1rst order Newton, I added a second-order to interpolation scheme.

    To be more practical I wrote a script that generates a sinewave with 375 kHz sampling frequency and transfers it to PC using 44.1 kHz USB Audio.

    general idea:
    Audio stream is controlled by PIT (class AudioTrigger)
    test class generates 375 kHz signal, which is placed on a circular buffer.
    an Audio Interface fetches data (here 375 kHz sampled sine wave) from audio buffer and transfers them into Audio scheme to be passed via USB to PC

    Obviously, the non-Audio-Lib processing could be done in floating point (on T3.5/3.6) but the exercise was integer only arithmetic (at least for the AudioInterface class)

    the sketch directory of a complete test program is in

    the resulting PSD is
    Click image for larger version. 

Name:	SinePSD.jpg 
Views:	91 
Size:	28.7 KB 
ID:	10775

    Some issues:
    Assumed that I send only one channel (left) and have second channel (right) free. But PC gives same signal on both channels. Need to investigate
    there are small modulations visible in the spectrogram, maybe due to non-correct signal generation. Need to understand origin of these modulations (or phase jumps).

    Edit: corrected some indexing
    generated cleaner PSD
    Click image for larger version. 

Name:	SinePSD2.jpg 
Views:	103 
Size:	24.5 KB 
ID:	10779
    Last edited by WMXZ; 06-10-2017 at 07:34 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2019
    I just found this post today while playing with sox under Linux (Raspberry Pi)
    I tried realtime upsampling (with Squeezelite) with Raspberry Pi (two cores dedicated to the upsampling) but it faills to do it in realtime, CPU 100%

    I wonder if it is possible to upsample on the fly 44K1Hz I2S audio to 188K4Hz I2S or even 356K8Hz I2S with Teensy 3.5 ?

    Are there any usable "bloc" to test different upsampling scheme (like with sox minimal phase, etc) ?

    Would it be possible to use this library ( to perform on the fly I2S upsampling ?

    Thank you for your answers,
    Last edited by bricolodu13; 06-09-2020 at 11:31 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts