My objective is to generate sine, square, triangle and sawtooth signals with Android's AudioTrack Class.
I have made an attempt based on a tutorial. It works relatively well, although I'm not always sure how accurate the frequency generated corresponds with the frequency aimed at.
I would like to make square, triangle etc. functions. The tutorial only implements sine. My question is therefore:
How exactly does the sample work
  new Thread( new Runnable( ) 
  {
     public void run( )
     {                
        final float frequency = 440;
        float increment = (float)(2*Math.PI) * frequency / 44100; // angular increment for each sample
        float angle = 0;
        AudioDevice device = new AndroidAudioDevice( );
        float samples[] = new float[1024];
        while( true )
        {
           for( int i = 0; i < samples.length; i++ )
           {
              samples[i] = (float)Math.sin( angle );
              angle += increment;
           }
           device.writeSamples( samples );
        }            
     }
  } ).st开发者_运维知识库art();
Can I make the sine into a square like follows (using the signum function)?
samples[i] = (float)Math.signum(Math.sin( angle ));
Basically, I would like to fundamentally understand the samples being written, so I can generate various signals and also eventually superposition them.
Thank you for your time!
I know this is a year old but... Yes, signum should work fine. Ditch the angle and incrementing business.
This code superimposes two signals - a square and a triangle at different frequencies.
int rate = 44100;
AudioDevice device = new AndroidAudioDevice( );
float samples[] = new float[1024];
while( true )
{
    for( int i = 0; i < samples.length; i++ )
    {
        samples[i] = (float)(0.5*Math.signum(Math.sin(550*2*Math.PI*i/rate))+0.5*Math.asin(Math.sin(450*2*Math.PI*i/rate)));
    }
    device.writeSamples( samples );
}
Btw, a better way to do square is probably: ((550.0*2*i/rate)%2<0?-1:1)
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论