Page 2 of 2

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:54 pm
by Folknology
infiniteimprobability wrote:Looks like you are sending an int over but pwm is expecting a short..
Good catch!

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 2:56 pm
by JLS

Code: Select all


#include <platform.h>
#include <xs1.h>
#include "pwm.h"

#define SAMPLE_RATE 44100


port audio = XS1_PORT_1A;


long A=0x7e66;
long buf[3]={0,0x1209,0};
int val;


void sine (chanend pwm){


            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768 + buf[ 0 ];

            pwm <: val;

            delay_microseconds (10);

}


int main ( void )
{

    chan pwm;

    par {
        sine(pwm);
        pwm_server(pwm, audio, SAMPLE_RATE);
      }

    return 0;
}




Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:00 pm
by infiniteimprobability
We've all been there at some point! Debugging channel i/o mismatches isn't always obvious.. This is where interfaces are a lot nicer as you get type checking. Channels are quick and easy to code though.

JLS - what's happening here is that the channel buffer isn't being fully consumed by pwm, so when you try to output another 32b int, it throws an exception.. Which at least is better than locking up as you can work out what has happened in the debugger. Moral of the story - make sure inputs and outputs are in synch and keep the types the same.

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:01 pm
by Folknology
JLS you are missing the while loop in your sine function/task

Code: Select all

void sine (chanend pwm){
  while(1){
            buf[ 0 ] = ((A * buf[ 1 ]) >> 14 ) - buf[ 2 ];
            buf[ 2 ] = buf[ 1 ];
            buf[ 1 ] = buf[ 0 ];

            val = 32768 + buf[ 0 ];

            pwm <: val;

            delay_microseconds (10);
  }
}
Also try using delay_microseconds (200);

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:07 pm
by JLS
Working !!! :-)

Problem is short (int) and while

Many thanks all

Kamil

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:12 pm
by JLS
Highest samplerate (192 000) for pwm produce smoothest waveforms :-)

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:13 pm
by JLS
My next project try use same sine algo with AudioSlice

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:23 pm
by Folknology
You might want to go over some of the XC basics to get your head around the tasks/cores side of things.

regards
Al

Re: IIR sine wave help

Posted: Wed Feb 19, 2014 3:28 pm
by JLS
Usefull links

Thanks