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

Thread: How to use Teensy libraries to do signal processing

  1. #1
    Junior Member
    Join Date
    Apr 2017
    Posts
    6

    How to use Teensy libraries to do signal processing

    For our project our goal is to acquire body sounds from an acoustic stethoscope and do digital 1) filtering 2) noise reduction (static + heart sounds), and 3) amplifying of signals. The frequencies we're trying to detect ranges from 20 Hz to 2kHz.

    Here is a block diagram of our plan so far

    ADC -> Bandpass Filter -> noise reduction (we don't know how to do this yet) -> amplify -> DAC.

    Do you think this is possible and would editing the available libraries be enough?

    **With regards to filters, I've been seeing that the input and output keep on using the i2s. How do we use the same input (i2s) if we don't have the audio board?

    Please do suggest any possible methods/libraries/steps on how to go about this since we're new to coding and to teensy in general. thanks!
    Last edited by mitzi111; 04-02-2017 at 12:39 PM.

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,899
    If you look at the Audio library
    https://www.pjrc.com/teensy/gui/?info=AudioInputAnalog
    You can use the Teensy ADC directly, at cost of increased noise and needing input circuitry, which you will probably need anyway.

    Places to look for info would be the audio library tutorial and examples. May also be worth having a look at
    https://forum.pjrc.com/threads/40115-Teensy-Hearing-Aid

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Except for the noise reduction involving unspecified algorithms, you can do all this pretty easily with the audio library.

    Most of the examples use the audio board with I2S. Of course you can use the design tool to connect the analog input to an ADC pin. Details here (right side documentation panel).

    https://www.pjrc.com/teensy/gui/?info=AudioInputAnalog

    If you're just starting and learning, it might be worthwhile to just get the audio shield so you can do the tutorial without all the fiddling of adapting to use different input & output methods. Then again, if you just watch some of the video and play with the design tool, maybe you'll understand how to use it to connect different input & output. Or take a look at this recent thread:

    https://forum.pjrc.com/threads/42981...n-audio-shield

  4. #4
    Junior Member
    Join Date
    Apr 2017
    Posts
    6
    thanks for the reply!

    would you happen to know how the StateVariableFilter works? I'm not too sure why it's input says white noise. and where does it gets the input? Would it be safe to say that I could edit the input to make it filter our chosen input signal?

    /* Example of filtering audio noise with a resonant filter,
    using Mozzi sonification library.

    Demonstrates StateVariable() with acute resonance,
    which in this case requires the input signal level to be reduced
    to avoid distortion which can occur with sharp resonance settings.

    This sketch using HIFI mode is not for Teensy 3.1.

    IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be
    be changed from STANDARD mode to HIFI.
    In Mozz/mozzi_config.h, change
    #define AUDIO_MODE STANDARD
    //#define AUDIO_MODE HIFI
    to
    //#define AUDIO_MODE STANDARD
    #define AUDIO_MODE HIFI

    Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar),
    Check the Mozzi core module documentation for others and more detail

    3.9k
    pin 9 ---WWWW-----|-----output
    499k |
    pin 10 ---WWWW---- |
    |
    4.7n ==
    |
    ground

    Resistors are ħ0.5% Measure and choose the most precise
    from a batch of whatever you can get. Use two 1M resistors
    in parallel if you can't find 499k.
    Alternatively using 39 ohm, 4.99k and 470nF components will
    work directly with headphones.

    Mozzi help/discussion/announcements:
    https://groups.google.com/forum/#!forum/mozzi-users

    Tim Barrass 2012, CC by-nc-sa.
    */

    #include <MozziGuts.h>
    #include <Oscil.h>
    #include <tables/whitenoise8192_int8.h>
    #include <StateVariable.h>



    Oscil <WHITENOISE8192_NUM_CELLS, AUDIO_RATE> aNoise(WHITENOISE8192_DATA); // audio noise
    StateVariable <BANDPASS> svf; // can be LOWPASS, BANDPASS, HIGHPASS or NOTCH


    void setup(){
    startMozzi();
    aNoise.setFreq(1.27f*(float)AUDIO_RATE/WHITENOISE8192_SAMPLERATE); // * by an oddish number (1.27) to avoid exact repeating of noise oscil
    svf.setResonance(1); // 0 to 255, 0 is the "sharp" end
    svf.setCentreFreq(3500);
    }


    void updateControl(){
    }


    int updateAudio(){
    int input = aNoise.next()>>1; // shift down (ie. fast /) to avoid distortion with extreme resonant filter setting
    int output = svf.next(input);
    return output<<4; // shift up for HIFI resolution
    }


    void loop(){
    audioHook();
    }

  5. #5
    Junior Member
    Join Date
    Apr 2017
    Posts
    6
    "Except for the noise reduction involving unspecified algorithms, you can do all this pretty easily with the audio library."

    Would you happen to know any open source algorithms fit for teensy that can do the job?

  6. #6
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,899
    There really isn't an answer to that one, since without further information everything in a signal is the signal. The noise is in the eye of the beholder so there is no magic noise suppression. From the sounds of your initial brief you want to look at various high and low pass filtering to winnow down signals you don't want, possibly with a gain control system to mute heartbeat which your vague initial description suggests isn't wanted. So part of your processing would look for the heartbeat, and then either just mute all signal while it's present, or try to get clever activly suppressing it.

    Was also the reason for linking the teensy hearing aid, since there the designer is trying to extract speech from the background.

    Given the questions you may also want to look at supercollider or similar computer based audio tools and try to build your filter there where seeing what you did is much easier, then look to implement said filter into the teensy. Also means a much greater pool of answers to fire a search engine into.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Quote Originally Posted by mitzi111 View Post
    would you happen to know how the StateVariableFilter works?
    Perhaps you're thinking of the Mozzi library? It's a completely separate library from the Teensy Audio Library.

    You can use either of these on Teensy, but they aren't designed to work together, so it's important to understand they are not the same code base or even compatible with each other in any way.

  8. #8
    Junior Member
    Join Date
    Apr 2017
    Posts
    6
    Do you know how to set the sampling rate of teensy 3.5?

  9. #9
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,899
    If you are still trying to do this the hard way, by either using or reading source from
    https://forum.pjrc.com/threads/25532...for-Teensy-3-1
    If using Audio library 44khz is pretty much fixed unless you want to have some serious adventures.

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Quote Originally Posted by mitzi111 View Post
    Do you know how to set the sampling rate of teensy 3.5?
    If you're using Mozzi, this question would be best to ask on Mozzi's forum.

    https://groups.google.com/forum/#!forum/mozzi-users

    Or you could start digging through the Mozzi source code to see if you can find it and figure out how to change the timing.

  11. #11
    Junior Member
    Join Date
    Apr 2017
    Posts
    6
    "speed can be any from the ADC_SAMPLING_SPEED class enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED or VERY_HIGH_SPEED.
    ADC_SAMPLING_SPEED::VERY_LOW_SPEED is the lowest possible sampling speed (+24 ADCK). (ADCK is the ADC clock speed, see below).
    ADC_SAMPLING_SPEED::LOW_SPEED adds +16 ADCK.
    ADC_SAMPLING_SPEED::MED_SPEED adds +10 ADCK.
    ADC_SAMPLING_SPEED::HIGH_SPEED adds +6 ADCK.
    ADC_SAMPLING_SPEED::VERY_HIGH_SPEED is the highest possible sampling speed (0 ADCK added)."

    I'm a little bit confused. Why does the very low speed gets the highest number of added ADCK? Also is there a specific value of ADCK to which you add the number based on the sampling speed you chose?

    If I were to use a max of 2khz frequency, it's alright to use the 44 kHz sampling rate right? Since the nyquist theory states that it should at LEAST be 2x the max freq?


    Lastly, (question's about filters, sorry if its far from the initial topic) what does stage and q means for this syntax in the filter example?

    "biquad1.setLowpass(0,600,0.707); //stage, freq, Q"

    thanks!

  12. #12
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,899
    Re your second question did you try reading up on filters?
    https://en.wikipedia.org/wiki/Q_factor
    https://en.wikipedia.org/wiki/Digital_biquad_filter

  13. #13
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Quote Originally Posted by mitzi111 View Post
    Why does the very low speed gets the highest number of added ADCK?
    The more clock cycles something uses, the longer it takes, so the speed is slower.

  14. #14
    Member
    Join Date
    Dec 2017
    Location
    Ohio
    Posts
    32
    I'm new to this Forum, so forgive me if this is an old question. Being new, I do not know the URL for some of the libraries being discussed and where I can go to downloaded them, like:

    #include <MozziGuts.h>
    #include <Oscil.h>
    #include <tables/whitenoise8192_int8.h>
    #include <StateVariable.h>


    I have a convention of placing the URL for non-standard libraries after the #include directive, like:

    #include <EEPROM.h> // Standard with IDE
    #include <SD.h> // Standard with IDE
    #include <Wire.h> // Standard with IDE
    #include <SPI.h> // Standard with IDE
    include <Encoder.h> // https://github.com/PaulStoffregen/Encoder
    #include "MorseCode.h" // Part of this project file. MUST be in same directory as this source file


    As a newbie, should I know where the above-mentioned libraries are?
    Last edited by econjack; 12-12-2017 at 12:11 AM.

  15. #15
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Most of that looks like the Mozzi library.

  16. #16
    Member
    Join Date
    Dec 2017
    Location
    Ohio
    Posts
    32
    Quote Originally Posted by PaulStoffregen View Post
    Most of that looks like the Mozzi library.
    Thanks, Paul, you're right. I donated even before the download just because of the volume of work that must have been involved in writing it.

    Jack, W8TEE

  17. #17
    Member
    Join Date
    Apr 2018
    Location
    Eastern Time, USA
    Posts
    72
    This might be helpful for your project. It is starts with low noise. I can probably help you with the signal processing also.

    https://forum.pjrc.com/threads/53173...-Teensyhttp://

  18. #18
    Member
    Join Date
    Dec 2017
    Location
    Ohio
    Posts
    32
    Quote Originally Posted by DrM View Post
    This might be helpful for your project. It is starts with low noise. I can probably help you with the signal processing also.

    https://forum.pjrc.com/threads/53173...-Teensyhttp://
    We ended up with a very nice DSP project as an addition to the µBITX amateur radio transceiver. The Teensy 3.6 and the audio board handle all of the touch screen tasks, digital processing, IF and audio filters, electronic keying and other tasks. Our rig looks like this:

    Click image for larger version. 

Name:	SmallJackAl.jpg 
Views:	36 
Size:	53.6 KB 
ID:	16286

    11,000+ lines of code and the Teensy just plows right through it.

Tags for this Thread

Posting Permissions

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