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

Thread: invert phase audio signal

  1. #1
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44

    invert phase audio signal

    Hello,

    I would like to reverse the phase of the audio signal (180 ) to experiment around the cancellation of sounds.

    I tried with a mixer to set the gain to -1 (i tried too with the amp), but the signals are not inverted as shown in the oscillogram.
    (sine signal 2K700hz 1vp-p, red : input, blue : ouptut).

    if i change the frequency, the offset between input and output changes, but is never 180 degrees

    Click image for larger version. 

Name:	invert_phase.jpg 
Views:	33 
Size:	73.2 KB 
ID:	23173

    my code :
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioInputI2S            i2s2;           //xy=214,235
    AudioMixer4              mixer1;         //xy=547,366
    AudioOutputI2S           i2s1;           //xy=879,224
    AudioConnection          patchCord1(i2s2, 0, mixer1, 0);
    AudioConnection          patchCord2(mixer1, 0, i2s1, 0);
    AudioControlSGTL5000     sgtl5000_1;     //xy=857,517
    // GUItool: end automatically generated code
    
    
    
    const int myInput = AUDIO_INPUT_LINEIN;
    //const int myInput = AUDIO_INPUT_MIC;
    
    void setup() {
      // put your setup code here, to run once:
      AudioMemory(50);
      // Enable the audio shield. select input. and enable output
    
      sgtl5000_1.enable();
      sgtl5000_1.inputSelect(myInput);
      sgtl5000_1.volume(0.4); // 
    
      mixer1.gain(0,-1);
      mixer1.gain(1,0);
      mixer1.gain(2,0);
      mixer1.gain(2,0);  
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }
    Do you know if there is another way?

    thanks in advance

    Fred (Teensy v4.0+ audio Adapter)

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    if you insert an audio object then you have to consider a 128 sample delay.
    So, insert two mixers in parallel one with +1 and the other with -1, then you should get the desired effect

  3. #3
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Quote Originally Posted by WMXZ View Post
    if you insert an audio object then you have to consider a 128 sample delay.
    So, insert two mixers in parallel one with +1 and the other with -1, then you should get the desired effect
    if I understand correctly, I put an audio object (microphone), connected to a delay object, then then two mixers?

  4. #4
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    no, I was suggesting
    i2s ->mixer (+1) -> I2S left
    |-> mixer (-1) -> I2S right
    so left and right path have same internal delays

  5. #5
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    I understand the 1st line connection, but Im not sure for the 2nd : what is *I* on the 2nd line ?
    (Sorry Im a newbie).

    I only have one audio source (microphone or low frequency generator)

  6. #6
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    sorry about formating
    here is the audio code
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioInputI2S            i2s1;           //xy=182.0023956298828,239.00240325927734
    AudioAmplifier           amp1;           //xy=375.0023880004883,195.00239181518555
    AudioAmplifier           amp2;           //xy=377.002384185791,256.0023946762085
    AudioOutputI2S           i2s2;           //xy=573.0023956298828,217.00240325927734
    AudioConnection          patchCord1(i2s1, 0, amp1, 0);
    AudioConnection          patchCord2(i2s1, 1, amp2, 0);
    AudioConnection          patchCord3(amp1, 0, i2s2, 0);
    AudioConnection          patchCord4(amp2, 0, i2s2, 1);
    // GUItool: end automatically generated code
    I use amp instead of mixer; amp1 is to be set gain +1, and amp2 is gain -1
    you can copy and import the above lines into audio gui to see what I mean.

  7. #7
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Click image for larger version. 

Name:	screen3.jpg 
Views:	17 
Size:	16.4 KB 
ID:	23180

    ok, now can you tell me :
    - where i connect my audio signal (sinusoidale 1khz with an external generator) : left or right or both line in ?
    - where i connect my oscilloscope : left or right line out ?

    thanks

  8. #8
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    Quote Originally Posted by fred_france View Post
    Click image for larger version. 

Name:	screen3.jpg 
Views:	17 
Size:	16.4 KB 
ID:	23180

    ok, now can you tell me :
    - where i connect my audio signal (sinusoidale 1khz with an external generator) : left or right or both line in ?
    - where i connect my oscilloscope : left or right line out ?

    thanks
    the way it is wired now, connect audio signal to both line in and oscilloscope channel 1 to ,say, left and channel 2 to right
    you could simplify, by connecting both amp inputs to same i2s input channel (left or right)

  9. #9
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    By connecting the two channels of the oscilloscope on L and R of line Out, it works well, the signals are well opposite at 180 (see the image below)

    Click image for larger version. 

Name:	screen_5.jpg 
Views:	13 
Size:	40.8 KB 
ID:	23182

    But if I plug in the oscilloscope like this:
    - oscillo channel 1: on the input (on the sine generator)
    - oscillo channel 2: on one of the LineOut outputs
    > the signals are out of phase but not at 180 (same as what i show in my post #1)

    I think there is a processing time which makes it impossible to reverse the input signal in real time
    Am I clear in my explanation?

  10. #10
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    Quote Originally Posted by fred_france View Post
    But if I plug in the oscilloscope like this:
    - oscillo channel 1: on the input (on the sine generator)
    - oscillo channel 2: on one of the LineOut outputs
    > the signals are out of phase but not at 180 (same as what i show in my post #1)

    I think there is a processing time which makes it impossible to reverse the input signal in real time
    Am I clear in my explanation?
    They cannot be 180 deg out of phase. there is a fixed travel time through the audio library
    Output is NEVER without delay to input.

    AFAIK
    I2S-input: data are buffered for 128 samples (2.9 ms @44.1 kHz)
    Amp: process data block-wise (another 2.9 ms)
    I2S-output: buffered data are output (no further delay)

    But you can mesure with oscilloscope

    The way your picture is made, both data streams are delayed the same amount of time but one is flipped, so you get the desired result.

  11. #11
    Senior Member
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    188
    Quote Originally Posted by WMXZ View Post
    The way your picture is made, both data streams are delayed the same amount of time but one is flipped, so you get the desired result.
    He is trying to experiment with NOISE CANCELLING. Broadcasting an inverted signal in an attempt to cancel out ambient noise.
    Due to the inevitable processing delay, will not work by playing output through a speaker to cancel ambient noise.
    Lotus played round with this about 10 years ago. Not sure how successful they were.

    Will work if used for a headphone where ambient and cancel signal are both delayed by the same or similar amount.
    Of course the headphones need to be isolated from ambient noise otherwise a sort of echo might ensue.

    On the other hand just isolate the headphones from the environment (over ear headphones) and play whatever you want to through headphones.

  12. #12
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    Thank you for your answers
    is it not possible to reduce the processing time of 2.9 ms by reducing for example the bandwidth? (for example: 200hz- 2khz instead of 20hz-20khz)?

  13. #13
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,151
    Quote Originally Posted by fred_france View Post
    Thank you for your answers
    is it not possible to reduce the processing time of 2.9 ms by reducing for example the bandwidth? (for example: 200hz- 2khz instead of 20hz-20khz)?
    The 2.9 ms is result of acquisition buffer (128 samples) and sampling rate (44.1 kHz) so you must change one of the two values to change the delay.
    This requires changing some basic parameters. It can be done, but not sure if it brings you forward.

    What ever you do in synchron parallel audio processing (one channel straight, other channel phase shifted), you must make sure that parallel paths have same delay.

  14. #14
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    533
    If your *only* goal is to create an inverted audio signal [and no other processing], then I would go for opamps.
    See here for basic circuits. One inverting opamp and one non-inverting opamp and feed them both with the same input signal.
    With a high-speed opamp you end up with an input-to-output delay in the sub-microsecond range.

    Paul

  15. #15
    Member
    Join Date
    Jan 2021
    Location
    France
    Posts
    44
    yes I saw that there is a lot of electronic diagram with the op amps, I will look at that.

    thank you all very much for your help and your time

Posting Permissions

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