DSPRelated.com
Forums

signal delay via PC soundcard?

Started by falke May 10, 2005
Hi everybody,

I am sorry if my question appears stupid here, but I could not

find anything helpful looking around the mighty web...

I am wether an expert in digital sound processing nor in electronics.

My problem:

I would like to generate two periodic signals of and synchronized to the mains

frequency with independently tunable phases and amplitudes, resp.

My idea was to feed a signal proportional to the mains AC into a PCs soundcard

and then use some software to delay it, do some nonlinear filtering to change

the signal form, and put it to the line-out. For this purpose I have an

PIII / 512MB under W2k where not more than approx. 20% of CPU load should be

generated by that job. The data sampling should be around 10K/sec or better

(since we might want to have something else than a simple sine in the output).

However, I could not find  any useful piece of software: PC based signal

generators I  found are never able to synch their output to an external signal,

effect software pretends to do a delay adjustable in ms steps, but comparing the

input and output when employing such programs shows a lot of funny things but

never a defined phase shift between both.

Is there any advice for a quick solution (I did some C programming under Linux,

butnothing  related to audio, and some TurboPascal in good old MSDOS days, so if

programming is required I would appreciate some advice how to read and write data

from/ to the sound card)? I'd appreciate as well any hints if there are better

solutions than using a PC.

Thanks in advance for any help

Uwe



falke wrote:

> Hi everybody, > > I am sorry if my question appears stupid here, but I could not > > find anything helpful looking around the mighty web... > > I am wether an expert in digital sound processing nor in electronics. > > My problem: > > I would like to generate two periodic signals of and synchronized to the mains > > frequency with independently tunable phases and amplitudes, resp. > > My idea was to feed a signal proportional to the mains AC into a PCs soundcard > > and then use some software to delay it, do some nonlinear filtering to change > > the signal form, and put it to the line-out. For this purpose I have an > > PIII / 512MB under W2k where not more than approx. 20% of CPU load should be > > generated by that job. The data sampling should be around 10K/sec or better > > (since we might want to have something else than a simple sine in the output). > > However, I could not find any useful piece of software: PC based signal > > generators I found are never able to synch their output to an external signal, > > effect software pretends to do a delay adjustable in ms steps, but comparing the > > input and output when employing such programs shows a lot of funny things but > > never a defined phase shift between both. > > Is there any advice for a quick solution (I did some C programming under Linux, > > butnothing related to audio, and some TurboPascal in good old MSDOS days, so if > > programming is required I would appreciate some advice how to read and write data > > from/ to the sound card)? I'd appreciate as well any hints if there are better > > solutions than using a PC. > > Thanks in advance for any help > > Uwe > > >
The problem is that the soundcard hardware just isn't built for synchronization that way. What you _could_ do, assuming that the soundcard's two input channels are synchronized (which they probably are, but I refuse to guarantee), is feed the power-line sample into one channel, one of your outputs into the other, and implement a phase-locked loop to get the signals synchronized. ------------------------------------------- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Tim Wescott wrote:

(snip regarding synchronizing the outputs of a PC sound card to
a supplied input signal.)

> The problem is that the soundcard hardware just isn't built for > synchronization that way. What you _could_ do, assuming that the > soundcard's two input channels are synchronized (which they probably > are, but I refuse to guarantee), is feed the power-line sample into one > channel, one of your outputs into the other, and implement a > phase-locked loop to get the signals synchronized.
I am not sure about PC sound cards, either, but it has been common practice for CD players to use one D/A converter multiplexed between channels. This results in a phase shift between channels that some claim is audible, but most don't believe it. (Unless you are very careful where you put your head.) -- glen
glen herrmannsfeldt wrote:
> Tim Wescott wrote: > > (snip regarding synchronizing the outputs of a PC sound card to > a supplied input signal.) > >> The problem is that the soundcard hardware just isn't built for >> synchronization that way. What you _could_ do, assuming that the >> soundcard's two input channels are synchronized (which they probably >> are, but I refuse to guarantee), is feed the power-line sample into >> one channel, one of your outputs into the other, and implement a >> phase-locked loop to get the signals synchronized. > > > I am not sure about PC sound cards, either, but it has been common > practice for CD players to use one D/A converter multiplexed between > channels. This results in a phase shift between channels that some > claim is audible, but most don't believe it. (Unless you are very > careful where you put your head.) > > -- glen >
Egad. True, but you could probably assume it's no more than 1/(2 * sample rate). ------------------------------------------- Tim Wescott Wescott Design Services http://www.wescottdesign.com
"glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
news:X5ednRx6_77xZR3fRVn-rA@comcast.com...
> Tim Wescott wrote: > > (snip regarding synchronizing the outputs of a PC sound card to > a supplied input signal.) > > > The problem is that the soundcard hardware just isn't built for > > synchronization that way. What you _could_ do, assuming that the > > soundcard's two input channels are synchronized (which they probably > > are, but I refuse to guarantee), is feed the power-line sample into one > > channel, one of your outputs into the other, and implement a > > phase-locked loop to get the signals synchronized. > > I am not sure about PC sound cards, either, but it has been common > practice for CD players to use one D/A converter multiplexed between > channels. This results in a phase shift between channels that some > claim is audible, but most don't believe it. (Unless you are very > careful where you put your head.)
This was definitely done in the past, but with so many cheap integrated stereo DACs available now, I don't think this has been a common practice for quite some time now. I doubt if any current products use this except for maybe your bottom-of-the-barrel $20 portable CD players or $40 CD boom boxes.
Jon Harris wrote:

> "glen herrmannsfeldt" wrote in message
>>I am not sure about PC sound cards, either, but it has been common >>practice for CD players to use one D/A converter multiplexed between >>channels. This results in a phase shift between channels that some >>claim is audible, but most don't believe it. (Unless you are very >>careful where you put your head.)
> This was definitely done in the past, but with so many cheap integrated stereo > DACs available now, I don't think this has been a common practice for quite some > time now. I doubt if any current products use this except for maybe your > bottom-of-the-barrel $20 portable CD players or $40 CD boom boxes.
There are now DVD players selling for $30 retail. How much do the parts cost? How much of the price goes to DACs? I don't claim to understand electronics marketing at all, but there has to be a lot of incentive to keep parts cost down. The last portable CD player I bought was $10. -- glen
Uwe,

i will try to make it short:

1) What you plan can, can be done.

2) Tim was already on the correct way: You need to extract frequency and
phase from a mains-proportional signal. This is best done with a digital
pll. The digital "vco" should be a DDS style oscillator. With this you will
be able to implement a pc-internal signal source that is phase coupled to
the mains. Because you have the frequency as well as the phase of the
pc-internal  source at hand, you will be able to generate every arbitrary
waveform out of it.

3) What you plan is not really common business, so expect to make all the
(programming) work yourself!

4) I can supply you a pascal source that shows how to get chunks of
soundcard samples in buffers of user defined size. Every once a buffer is
full, a callback function of your program is called, which now may process
the contents of your buffer.You should be able to translate it into c.

5) Try to inform yourself about the terms "digital pll" and "DDS". I can
help you regarding some aspects of your problem but i can not do all the
work!

Regards
Ulrich

P.S.

Once you have a signal being phase-coupled to mains, any signal delay in the
soundcard becomes irrelevant because you can generate a signal at ANY phase
delay with regard to mains just by adding a constant to se so-called phase
accumulator. Understanding what a "phase accumulator" is, is part of your
homework!


"falke" <falke@liv.ac.uk> schrieb im Newsbeitrag
news:4280E1F1.18A375A0@liv.ac.uk...
> Hi everybody, > > I am sorry if my question appears stupid here, but I could not > > find anything helpful looking around the mighty web... > > I am wether an expert in digital sound processing nor in electronics. > > My problem: > > I would like to generate two periodic signals of and synchronized to the
mains
> > frequency with independently tunable phases and amplitudes, resp. > > My idea was to feed a signal proportional to the mains AC into a PCs
soundcard
> > and then use some software to delay it, do some nonlinear filtering to
change
> > the signal form, and put it to the line-out. For this purpose I have an > > PIII / 512MB under W2k where not more than approx. 20% of CPU load should
be
> > generated by that job. The data sampling should be around 10K/sec or
better
> > (since we might want to have something else than a simple sine in the
output).
> > However, I could not find any useful piece of software: PC based signal > > generators I found are never able to synch their output to an external
signal,
> > effect software pretends to do a delay adjustable in ms steps, but
comparing the
> > input and output when employing such programs shows a lot of funny things
but
> > never a defined phase shift between both. > > Is there any advice for a quick solution (I did some C programming under
Linux,
> > butnothing related to audio, and some TurboPascal in good old MSDOS days,
so if
> > programming is required I would appreciate some advice how to read and
write data
> > from/ to the sound card)? I'd appreciate as well any hints if there are
better
> > solutions than using a PC. > > Thanks in advance for any help > > Uwe > > >
Ulrich Bangert wrote:

Dear Ulrich ,
thank you very much for your helpful response so far.


> 2) Tim was already on the correct way: You need to extract frequency and > phase from a mains-proportional signal. This is best done with a digital > pll. The digital "vco" should be a DDS style oscillator. With this you will > be able to implement a pc-internal signal source that is phase coupled to > the mains. Because you have the frequency as well as the phase of the > pc-internal source at hand, you will be able to generate every arbitrary > waveform out of it.
To see if I am right: PLL is a phase locked loop. In my case it would need to cross correlate the input signal with a internally generated 50Hz sine to detect (and maybe correct) the phase shift, but a simple detection of the positive zero crossing of the (low-pass filtered) input signal might be sufficient here, might it not? I understand you so that I should create an internal signal being phase coupled to the input. However, if I use just the information when the trigger point (0 crossing) repeatedly occurs I have all at hand what I need: phase and frequency. Do I really need a physically existent reference signal anyway? I thought I could either read the input signal into a ring buffer, or put a predefined signal sample there, and put that to the sound output with an adjustable offset against the trigger condition detected in the input which would define the phase between input and output. To explain the background in short: In a highly resolving scanning electron microscope we see a remaining 50Hz distortion of the position of the electron beam (spatial amplitude around 0.05nm). Despite our desperate hunt for ground loops, earth currents and whatever, we could not get rid of it. We plan therefore to actively compensate this be adding a 50Hz signal to the magnetic coils scanning the beam over the sample. However, the distortion does not seem completely sine-like, thus we would need probably a more complicated output, which may anyway be measured once and then reused. The output signal could then be a specially designed signal sample covering 20ms in time and repeated whenever the trigger condition is detected where the phase could be either implemented by a delay or by selection of the proper offset in the ring buffer holding the signal sample. It would not matter too much if there are slight deviations of the mains frequency of 50Hz as long it is below say 1% which would mean an accumulating phase error of below 0.06. Alternatively on could adjust the sample rate for the output so that the whole sample covers exactly one period of the input signal but I do not know if the sample rate in PC sound systems may be set in such fine steps.
> > > 4) I can supply you a pascal source that shows how to get chunks of > soundcard samples in buffers of user defined size. Every once a buffer is > full, a callback function of your program is called, which now may process > the contents of your buffer.You should be able to translate it into c.
I'd very much appreciate if you could provide me with some code examples how to get the input signal to bytes, and bytes to make an output signal.
> > > 5) Try to inform yourself about the terms "digital pll" and "DDS". I can > help you regarding some aspects of your problem but i can not do all the > work! > > Once you have a signal being phase-coupled to mains, any signal delay in the > soundcard becomes irrelevant because you can generate a signal at ANY phase > delay with regard to mains just by adding a constant to se so-called phase > accumulator. Understanding what a "phase accumulator" is, is part of your > homework!
I just read that by phase accumulator an integer register is meant which is counted up, and where each increment means a certain step in phase. This I do not quite understand: If I have such a register, clocked with a defined clock, then I can only generate a signal with one single frequency as defined by he phase increment. To generate a signal containing several frequencies, I would need different phase accumulators driven either by different clocks or using different phase increments. Somewhere I read that the p.a. provides a pointer into a sample buffer. Then it is no more a true p.a., but rather a time accumulator, since a unique phase cannot be defined for a signal containing more than one frequency. Anyway, would I have to implement such a p.a. driven DDS in software? I thought, the sound system provides all I need: the capability to control output amplitude over time by sending the proper byte stream to the system starting output at the proper time and clocking the output with the proper rate. Thanks again, Uwe
> To see if I am right: > PLL is a phase locked loop. In my case it would need to cross correlate the > input signal with a internally generated 50Hz sine to detect (and maybe > correct) the phase shift, but a simple detection of the positive zero crossing > of the (low-pass filtered) input signal might be sufficient here, might it not?
uhm ... I understood something else (for the general setup) the PC has some input and output delay but I think the delay for the left and right channel of the same input is zero. You should use the pll for compensating against that delay! You could use both inputs - one gets the reference and the other a looback of you generated output signal. your software (PLL) just shifts the outputphase so that both inputs are in-phase ... for the PLL: how you build you phase detector - this is another question. you could just measure the distance of the zero crossings or use s.th like sum of the absolute of the difference of both waveforms usually a pll would have a lowpass filter then ... (just use some averaging to get rid of noise) next step would be the oscillator - the idea was to use a DDS here ... No idea if your idea of a fixed waveform is accurate enough ... the oscillator then would just shift the waveform bye, Michael
falke wrote:

> Ulrich Bangert wrote: > > Dear Ulrich , > thank you very much for your helpful response so far. > > > >>2) Tim was already on the correct way: You need to extract frequency and >>phase from a mains-proportional signal. This is best done with a digital >>pll. The digital "vco" should be a DDS style oscillator. With this you will >>be able to implement a pc-internal signal source that is phase coupled to >>the mains. Because you have the frequency as well as the phase of the >>pc-internal source at hand, you will be able to generate every arbitrary >>waveform out of it. > > > To see if I am right: > PLL is a phase locked loop. In my case it would need to cross correlate the > input signal with a internally generated 50Hz sine to detect (and maybe > correct) the phase shift, but a simple detection of the positive zero crossing > of the (low-pass filtered) input signal might be sufficient here, might it not?
It might, but be prepared to go ahead and multiply the power-line input by a sine wave to get a "real" phase estimate.
> > I understand you so that I should create an internal signal being phase > coupled to the input. However, if I use just the information when the trigger > point (0 crossing) repeatedly occurs I have all at hand what I need: phase > and frequency. Do I really need a physically existent reference signal > anyway?
You want to reference the phase to the power line, right?
> I thought I could either read the input signal into a ring buffer, or put a > predefined signal sample there, and put that to the sound output with an > adjustable offset against the trigger condition detected in the input which > would define the phase between input and output. > > To explain the background in short: In a highly resolving scanning electron > microscope we see a remaining 50Hz distortion of the position of the > electron beam (spatial amplitude around 0.05nm). Despite our desperate > hunt for ground loops, earth currents and whatever, we could not get rid > of it. We plan therefore to actively compensate this be adding a 50Hz > signal to the magnetic coils scanning the beam over the sample. > However, the distortion does not seem completely sine-like, thus we > would need probably a more complicated output, which may anyway be > measured once and then reused. > The output signal could then be a specially designed signal sample > covering 20ms in time and repeated whenever the trigger condition > is detected where the phase could be either implemented by a delay or > by selection of the proper offset in the ring buffer holding the signal > sample. It would not matter too much if there are slight deviations > of the mains frequency of 50Hz as long it is below say 1% which > would mean an accumulating phase error of below 0.06. > Alternatively on could adjust the sample rate for the output so that > the whole sample covers exactly one period of the input signal but > I do not know if the sample rate in PC sound systems may be set in > such fine steps. > > >> >>4) I can supply you a pascal source that shows how to get chunks of >>soundcard samples in buffers of user defined size. Every once a buffer is >>full, a callback function of your program is called, which now may process >>the contents of your buffer.You should be able to translate it into c. > > > I'd very much appreciate if you could provide me with some code examples how > to get the input signal to bytes, and bytes to make an output signal. > > >> >>5) Try to inform yourself about the terms "digital pll" and "DDS". I can >>help you regarding some aspects of your problem but i can not do all the >>work! >> >>Once you have a signal being phase-coupled to mains, any signal delay in the >>soundcard becomes irrelevant because you can generate a signal at ANY phase >>delay with regard to mains just by adding a constant to se so-called phase >>accumulator. Understanding what a "phase accumulator" is, is part of your >>homework! > > > I just read that by phase accumulator an integer register is meant which is > counted up, and where each increment means a certain step in phase. This > I do not quite understand: If I have such a register, clocked with a defined > clock, then I can only generate a signal with one single frequency as defined > by he phase increment.
"Phase" in this case is just the offset into a repetative signal, not necessarily a sine wave. As such you could call it a "time accumulator", but the accepted usage is "phase accumulator".
> To generate a signal containing several frequencies, I > would need different phase accumulators driven either by different clocks or > using different phase increments. Somewhere I read that the p.a. provides a > pointer into a sample buffer. Then it is no more a true p.a., but rather a time > accumulator, since a unique phase cannot be defined for a signal containing > more than one frequency. > Anyway, would I have to implement such a p.a. driven DDS in software? I > thought, the sound system provides all I need: the capability to control > output amplitude over time by sending the proper byte stream to the system > starting output at the proper time and clocking the output with the proper > rate. >
You need to implement a phase-error-accumulator, so you can line up your intended output with the power line. ------------------------------------------- Tim Wescott Wescott Design Services http://www.wescottdesign.com