tags:

views:

216

answers:

2

How to generate an efficient square waveform with varying duty cycle using C language?

+1  A: 

Choose your output format. Headerless PCM is probably best to start with. Select your output format - say, 16 bit stereo at 44 KHz. Choose your endianness. Write a bit of code which emits to a file logical 0 for 1 seconds worth of data; then emits logical 65535 for one second worth of data. Repeat.

That file contains your waveform.

Blank Xavier
Or do you mean a video waveform?
Blank Xavier
That doesn't sound as if it addresses the "varying duty cycle" requirement, though.
unwind
Well, the '1 seconds worth of data' repeated is the duty cycle. The good news is the audio will compress rather well.
Jonathan Leffler
Does he rather mean that it should output this Live(straight to the sound card) and in a multitasking environment? Cause that would be a much less trivial question
Earlz
+1  A: 

Let N be the cycle length (1 / frequency). N is a count of some small quanta, like clock ticks. Let D be the amount of quanta the output is high during each cycle of N. The algorithm is trivial:

loop indefinitely:
  for D ticks:
    output 1
  for N - D ticks:
    output 0
Eli Bendersky