IIR sine wave help

All technical discussions and projects around startKIT
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

infiniteimprobability wrote:Looks like you are sending an int over but pwm is expecting a short..
Good catch!

regards
Al


JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post 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;
}



User avatar
infiniteimprobability
XCore Legend
Posts: 1126
Joined: Thu May 27, 2010 10:08 am
Contact:

Post 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.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post 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
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

Working !!! :-)

Problem is short (int) and while

Many thanks all

Kamil
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

Highest samplerate (192 000) for pwm produce smoothest waveforms :-)
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

My next project try use same sine algo with AudioSlice
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post 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
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

Post by JLS »

Usefull links

Thanks
Post Reply