5
\$\begingroup\$

I created a two differrent signals on my android phone, one to act as the clock signal (lower signal), and the other to be the data signal (lower signal), but the problem is that the audio the signal always drifts towards the "ground"

signal

Why does it do this? Note that my signal isn't really a square wave it's more of a PWM.

\$\endgroup\$
5
  • \$\begingroup\$ Are these coming out of the headphone jack? \$\endgroup\$
    – Matt Young
    Mar 26, 2015 at 14:26
  • \$\begingroup\$ @MattYoung Yes they are. One's the left channel, and the other's the right channel \$\endgroup\$
    – Olumide
    Mar 26, 2015 at 14:27
  • \$\begingroup\$ Does this matter? As long as the receiver detects the rising edge of the pulse, the DC level does not matter. Or the receiver could take anything over 0.3Vmax as a 1 and under as a 0. \$\endgroup\$
    – user70961
    Mar 27, 2015 at 10:53
  • \$\begingroup\$ Excessive DC may damage some equipment. \$\endgroup\$ Mar 27, 2015 at 10:58
  • \$\begingroup\$ It does matter because if I have a cettain amount of consecutive 1s let's say 8, it would converge to 0, and if not for 8 it would for some other amount \$\endgroup\$
    – Olumide
    Mar 27, 2015 at 11:06

4 Answers 4

10
\$\begingroup\$

The signal from the headphone jack is most likely AC-coupled.

The easy way to fix this is to add a diode from each signal line to ground: anode of both diodes to ground, cathode of each diode to your signal line.

The diodes act as a clamp and allow the signal to NOT go lower than about -0.6V. The remainder of the signal amplitude is above ground (positive).

Note that this works ONLY if the output is AC-coupled.

The reason I mention this is that the headphone signal on your phone might be AC-coupled but phones of different models or from different manufacturers might NOT be.

There is a way to fix that, though.

\$\endgroup\$
2
  • \$\begingroup\$ There is a way to fix that, though. Perhaps some kind of modulation can be used (AM or FM) to transfer the signal (the signal seems to be around 100Hz, and I guess the max. audio frequency of a phone should be well above a few kHz). The modulated signal will be zero-centered. \$\endgroup\$
    – Sanchises
    Mar 26, 2015 at 18:24
  • \$\begingroup\$ No modulation needed. If you look at the waveform closely, you will see that the pulse shape is just fine. The only problem is the ground drift. This is easily fixed. \$\endgroup\$ Mar 26, 2015 at 23:29
11
\$\begingroup\$

That'll be because you're creating an audio signal - an audio signal is AC coupled, and will always want to drift towards ground since ground is what it is centred around.

An android phone's headphone socket cannot create reliable digital waveforms since it's an analog output. You should instead choose something that is suited to what you want, instead of trying to use something that is completely wrong and getting terrible results.

\$\endgroup\$
13
  • 1
    \$\begingroup\$ This is a great answer, except for your 2nd paragraph. There are a TON of commercial devices available that use the headphone connector on phones to pass data to and from the phone. From recent memory, two are: Ryobi Phone Tools -and- a Credit Card reader from a company called "Square". There are many more. \$\endgroup\$ Mar 26, 2015 at 14:50
  • 1
    \$\begingroup\$ @DwayneReid Well yes, but they are intended for use with analog communication signals. Using an analog signal generator to generate purely digital signals is not a good plan. You can do communication over the headphone connector, but you have to work in the analog domain, not the digital domain - i.e., something like OOK of an audio signal. \$\endgroup\$
    – Majenko
    Mar 26, 2015 at 15:01
  • 4
    \$\begingroup\$ I honestly don't want to argue with you - you generally give awesome answers and I respect you. Let me mention just one more example: modems. These allow reliable and robust digital communications over (sometimes) awful analog communication channels. The Internet we take for granted today wouldn't exist if modems interfaced to telco lines hadn't been invented. And there are so many more examples of audio / analog paths being used to pass digital information back and forth. If you were to edit your answer to remove the 2nd paragraph, I'll happily delete my comments. \$\endgroup\$ Mar 26, 2015 at 15:24
  • 7
    \$\begingroup\$ Technically, a digitial signal is an analog signal. And if you take into account the DC blocking capacitor usually used in headphone outputs, there are a wide variety of "digital" signals that can be faithfully reproduced using an "analog" headphone output. There are many, many more that can be adequately reproduced. The distinction between analog and digital is arbitrary, and your last paragraph is incorrect on a number of levels. \$\endgroup\$
    – Adam Davis
    Mar 26, 2015 at 15:41
  • 2
    \$\begingroup\$ I'm pretty sure that we are abusing the comment system to become a discussion board. I'm going to stop. I'll close with this: It's easy to make a DC-restorer circuit work with any phone: simply AC-couple the signal into the circuit as it comes from the phone. And - I think that helping the OP make his circuit work rather than bashing it would have been the better approach. \$\endgroup\$ Mar 26, 2015 at 16:02
4
\$\begingroup\$

To solve this problem you can instead modify the encoding so the sum of 1 and 0 bits actually sent over the wire converges to equal and the number of consecutive 1s and 0s is minimal. This means sending some extra bits over the wire but it will remove the DC component that the audio hardware can't handle. This means using a constant weight encoding or Paired disparity encoding.

Constant weight in it's simplest form that means sending a 01 for 0 and a 10 for a 1. This scheme has the obvious downside that desynchronization during decoding/capture means scrambling the entire message.

Going more advanced with paired disparity means creating 2 packets per symbol you want to send, one with more 0s and one with more 1 and then dynamically selecting based on the difference between the number of 1s and 0s already sent.

\$\endgroup\$
4
  • \$\begingroup\$ This was actually what I did after I learnt that it was AC coupling and what that was \$\endgroup\$
    – Olumide
    Mar 27, 2015 at 10:47
  • \$\begingroup\$ @Olumide If this is what you ended up doing, then this should probably the accepted answer, right? Not the one which suggests adding diodes which might or might not work. \$\endgroup\$
    – Fritz
    Mar 27, 2015 at 13:22
  • \$\begingroup\$ @fritz I asked for why the signal was behaving like that, not what to do, besides I don't think I can change the accepted answer \$\endgroup\$
    – Olumide
    Mar 27, 2015 at 13:25
  • \$\begingroup\$ @Olumide you can just click the checkmark again. \$\endgroup\$ Mar 27, 2015 at 13:29
2
\$\begingroup\$

You can compensate for this in software!

As you can see from the shape of the signal (the curve that is slowly pulling your signal to ground), there is a time component involved. You can offset it in software by emulating the time constant and compensating for it.

This ability will be slightly limited:

  • It will not be perfect at all frequencies
  • Long pulses will require more "headroom"

If you lower the base amplitude of the signal, the available headroom can be used for a longer time, giving you the ability to compensate for longer pulses. But you have to keep in mind that your time constant compensator will have to recenter itself on 0V eventually, otherwise the DC bias will eventually creep to maximum positive or negative amplitude and then clip and distort.

So the idea is to come up with a filter that will boost what the headphone amplifier is limiting.

smth

Here is an example in which I superimposed a triangle wave on top of the square wave. Note that this example is a simplification because the voltage of the triangle wave normally depends on the voltage of the square wave. This circuit will only work with that specific square wave.

The values were obtained empirically. The square wave at the left represents your signal. The two identical clusters on the right represent the capacitive coupling of your headphone amplifier. In the upper part of the circuit, the signal is sent through unaltered, resulting in the waveform in the first graph.

The lower part of the circuit adds a triangle wave on top of the signal. The phase is matched with the square wave. The op-amp part is simply a follower amplifier that attenuates the signal a bit so that the output is not too loud for the headphone amplifier.

By superimposing the triangle wave on the original signal, the output will look more like a square wave, like in the second graph.

The third graph shows the output before it is filtered by the capacitive coupling.

So what you should try to do is reimplement the triangle wave in software. Beware of the pitfalls!

Edit: and as Dwayne Reid pointed out, this capacitive coupling is specific to your phone. Other devices might have different characteristics, or not have coupling at all. So this is a solution for your device only.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.