IIR sine wave help

All technical discussions and projects around startKIT
JLS
Member++
Posts: 18
Joined: Fri Feb 14, 2014 11:21 am

IIR sine wave help

Postby JLS » Tue Feb 18, 2014 11:27 pm

Hi all

Is possible help me with simple example how to generate 1bit pwm audio output with this IIR sine wave algo ?

Code: Select all


int main ( void )
{

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

    while(1)
    {

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

            val = 128 + (buf[ 0 ] >> 8);

   }
}

Please help me with pwm audio output example.

Many thanks

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

Postby JLS » Wed Feb 19, 2014 12:04 pm

This is my example but not working produce error message - " warning: `c_pwm' not used in two parallel statements "

Code: Select all


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

#define SAMPLE_RATE 44100

on stdcore[0]: port audio = XS1_PORT_1A;

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


void xscope_user_init( void )
{
    xscope_register( 1
        ,XSCOPE_CONTINUOUS ,"signal" ,XSCOPE_INT ,"n");
}

int main ( void )
{

    chan c_pwm;

    while(1)
    {

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

            val = 32768+buf[ 0 ];

            xscope_int(0, val);

            c_pwm <: val;

            pwm_server(c_pwm, audio, SAMPLE_RATE);

            delay_microseconds (10);

    }

    return 0;
}


Please help me many thanks

Kamil
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Postby Folknology » Wed Feb 19, 2014 12:28 pm

You need to use two threads (or logical cores) to seperate the pwm and sine tasks

Rewrite your main as a function sine and remove the pwn server :

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 ];

            xscope_int(0, val);

            c_pwm <: val;

            delay_microseconds (10);
    }
}

Then create a new main with a par and your 2 tasks with a common channel between them :

Code: Select all

void main(void) {
  chan pwm;

  par {
    sine(pwm);
    pwm_server(pwm, audio, SAMPLE_RATE);
  }
}
I haven't checked the detail of your code..

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

Postby JLS » Wed Feb 19, 2014 1:12 pm

Testing it and compile without errors but not working !
You do not have the required permissions to view the files attached to this post.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Postby Folknology » Wed Feb 19, 2014 1:45 pm

Is xscope showing the correct waveform?

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

Postby JLS » Wed Feb 19, 2014 2:17 pm

yes sine algo working on xscope

but pwm not produce sound (this is same pwm like SID projects)
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Postby Folknology » Wed Feb 19, 2014 2:21 pm

Maybe try adjusting the relative rates of the tasks your sine rate could be higher than the PWM sample rate.

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

Postby JLS » Wed Feb 19, 2014 2:45 pm

compiling project OK when run this project generate this warning - "xrun: Program received signal ET_ILLEGAL_RESOURCE, Resource exception.
0x00010210 in pwm_server (c_samples=2147549698, p_pwm_output=66048, pwm_frequency=44100) at ../src/pwm.xc:33"
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Postby Folknology » Wed Feb 19, 2014 2:48 pm

Can you paste your code please using the code tag

regards
Al
User avatar
infiniteimprobability
XCore Legend
Posts: 1124
Joined: Thu May 27, 2010 10:08 am

Postby infiniteimprobability » Wed Feb 19, 2014 2:49 pm

Looks like you are sending an int over but pwm is expecting a short..

Who is online

Users browsing this forum: No registered users and 0 guests