+ 2

help: Android AudioTrack class

How can i generate square wave of specific frequency for specific period of time? As example : I want to generate 10Khz square wave for 100 milliseconds. How can i do this? please give me an example with detail explanation. Thanks in advance :)

6th Nov 2018, 11:42 AM
M.I.Rayhan
M.I.Rayhan - avatar
6 Answers
+ 7
10th Nov 2018, 5:58 PM
Biel Blue
Biel Blue - avatar
+ 1
Math is hard! So let me explain. There is a thing called Fourier Series that allow to calculate a ton of periodic functions and square waves are some of them. An actual square wave is either at maximum or at minimum, that is, only have two possible values. In code could be something like this: Sgn( x ): If x is less than 0 then return -1 If x is bigger than 0 then return 0 If x is zero return zero So the actual function would be Sgn(sin(frequency*2*Pi*X)) sin is sine X is the paramenter of the function If you think as an infinite sum of sine functions you will get this: X is in radians 4 * sin (1*X*frequency)/(1*Pi) + 4 * sin (3*X*frequency)/(3*Pi) + 4 * sin (5*X*frequency)/(5*Pi) + 4 * sin (5*X*frequency)/(5*Pi) + 4 * sin (7*X*frequency)/(7*Pi) + 4 * sin (9*X*frequency)/(9*Pi) + 4 * sin (11*X*frequency)/(11*Pi) + ... And so one
17th Nov 2018, 4:28 PM
Gabriel Quintela
Gabriel Quintela - avatar
0
thanks buddy, but there is still some problem remains. i know how to generate wave but i can't generate wave for specific time. eg: suppose i want to generate 38Khz square wave for 600 micro second. ok i am dividing my question into two parts: first: i want to generate square wave. second: i want to generate wave for specific time (less than 100 milli seconds). thank you :)
10th Nov 2018, 8:12 PM
M.I.Rayhan
M.I.Rayhan - avatar
0
I think you need to learn some physics, because a square wave is the sum o many sine wave in different wave lengths and phase (if i remember right). Any wave can be produced by this method. I hope it may help you
11th Nov 2018, 12:39 AM
Gabriel Quintela
Gabriel Quintela - avatar
0
@Gabriel Quintela Math.sin((frequency X 2 X Math.PI X sampleNumber) / sampleRate) this is a SINE wave equation. now convert this equation into SQUARE wave.
11th Nov 2018, 6:20 AM
M.I.Rayhan
M.I.Rayhan - avatar
0
@Gabriel Quintela unfortunately that doesn't work for me. i tried it before, i chaned freq value to high while freq greater than 0 and and to low while freq less than 0, like this: x = (freq>0)? 1: 0; I am actually trying to communicate my microcontroller to android through headphone jack. So my plan is to send pulses and decode them as 1 and 0. I already did it with IR, RF using interrupt, ccp module. Now i think you get it.
17th Nov 2018, 8:01 PM
M.I.Rayhan
M.I.Rayhan - avatar