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
2009-11-12 13:17:18
Or do you mean a video waveform?
Blank Xavier
2009-11-12 13:17:41
That doesn't sound as if it addresses the "varying duty cycle" requirement, though.
unwind
2009-11-12 13:21:23
Well, the '1 seconds worth of data' repeated is the duty cycle. The good news is the audio will compress rather well.
Jonathan Leffler
2009-11-12 13:47:47
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
2009-11-12 14:07:11
+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
2009-11-12 14:04:37