CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Ring Buffer to implement simple lowpass filter
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Greate the signal has improved
PostPosted: Thu Jul 03, 2008 5:40 am     Reply with quote

Great the signal has improved although the top/peaks are some how flat.
Maybe it has to do with my order. And yes I'm using the simulator.
What order are you using.

Here is what I'm using..
Code:
const int8 order = 32;
const int8 ORDER =64;
...........................
signal.new_ADC = (signed int16)(500*sin(12.6*t))); /*-500 to +500*/
................................................................


{
signal.Ring[tail] =  signal.new_ADC;
filter=&h1[order - tail];

for (i = 0; i < ORDER-1; i++)       
      {
        y += *(filter++) * Ring[i];
      }

       snew_ADC = (signed int16) y/4096;             
      filter_data_flag = 1;       
      tail = (tail + 1) & (order - 1);   
........................................
}
Ttelmah
Guest







PostPosted: Thu Jul 03, 2008 7:16 am     Reply with quote

Flat tops, suggests it is still clipping somewhere. What sort of value are you getting out at this point?.
I don't 'like' relying on case to separate the order number from the number of elements. It 'risks' something going wrong. look at how I did it, using order*2.
Try putting a bracket round the final maths. As shown, it risks casting 'y' to a signed int16, before the arithmetic, rather than the result after the arithmetic. So:
snew_ADC = (signed int16)(y/4096);

Best Wishes
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Here are the results
PostPosted: Thu Jul 03, 2008 7:48 am     Reply with quote

here are the results:
Code:
0
0
0
0
0
1
2
4
6
9
12
16
21
24
27
29
29
28
24
18
11
3
-4
-10
-15
-18
-18
-15
-10
-4
2
9
14
17
18
16
12
6
0
-7
-13
-17
-18
-17
-14
-8
-1
5
11
16
18
18
15
10
3
-3
-9
-14
-18
-18
-16
-12
-6
0
7
13
17
18
17
13
8
1
-5
-11
-16
-18
-18
-15
-9
-3
3
10
15
18
18
16
11
5
-1
-8
-13
-17
-18
-17
-13
-7
0
6
12
16
18
17
14
9
2
-4
-10
-15
-18
-18
-16
-11
-5
1
8
14
17
18
17
13
7
0
-6
-12
-16
-18
-17
-14
-9
-2
4
10
15
18
18
15
10
4
-2
-9
-14
-17
-18
-16
-12
-6
0
7
12
17
18
17
14
8
2
-4
-11
-15
-18
-18
-15
-10
-4
2
9
14
17
Ttelmah
Guest







PostPosted: Thu Jul 03, 2008 8:32 am     Reply with quote

Look, what I am suggesting is that you look at the real maths. Put a breakpoint inside the filter loop, run in the simulator, and actually look at the value arriving, what coefficient is selected for each value, and the resulting multiple. Do this for each entry round the filter loop, then repeat for each entry round the buffer, and you will see if it is selecting the correct coefficients, and whether the maths is clipping. The final output, is much too late, to see what is going on, you have to look _inside_ the code, to see where the problem appears.
No-one else is going to have the time, to look through the values you get I'm afraid...
I have used this technique in the past, for a smaller filter of this sort (only eight terms), but I selected the coefficients at the start, to fit inside the arithmetic, and to give results that scaled nicely to my final output (/256).

Best Wishes
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Yeah you are right
PostPosted: Fri Jul 04, 2008 2:38 am     Reply with quote

Sadly enough my coefficients are never selected. The pointer to the coefficient buffer (signed int16 *filter = &h1[32 - tail] does produce values that are not in h1[]; These looks like random values.I don't know what is wrong with it.
Code:

signal.Ring[tail] =  signal.new_ADC;
filter=&h1[order - tail];

for (i = 0; i < ORDER-1; i++)       
      {
        y += *(filter++) * Ring[i];
      }



Secondly, My adc values are arriving correctly. What I don't understand again is why do I've to double the size of Ring[(ORDER*2)+1] if the tail won't allow it fill past 32! in the simulator, the ring buffer fills from 1---31-0 and never fill beyond this range!
Ttelmah
Guest







PostPosted: Fri Jul 04, 2008 8:43 am     Reply with quote

The point about doubling the size of the coefficient list is this.
Imagine you had just a four entry buffer, and four coefficients, 1, 2, 3 & 4.
The buffer is 'full' and starting round the second time at index '0'
Code:

//fixed pitch font needed....

buffer entry       coefficent    tail 0
0                      0               
1                      1
2                      2
3                      3

//On the next call, you need: tail 1

0                      3
1                      0
2                      1
3                      2

//Next time you need: tail 2

0                      2
1                      3
2                      0
3                      1

//Finally you need : tail 3

0                      1
1                      2
2                      3
3                      0


Now, if you have a 'double length' coefficient list, containing:

0,1,2,3,0,1,2,3

Then you have in sequence, the required coefficient, for each possible 'tail' value, by selecting the four values starting at 4-tail in the list. You need the buffer double length, to allow for the four possible starting positions defined by 'tail'.

I'd not be fiddling around with two buffers. Just store the coefficients already calculated, and call them something more obvious, like 'coefficients'. &(coefficients[32-tail]), should successfully address the required first entry.
How old is your compiler?. The earlier CCS compilers, had an error in address arithmetic, incrementing pointers by '1', when you did address++, rather than by the size of the element. This would make the code not work (you would have to use: address=address+(sizeof(signed int16)). This might be your problem.

Best Wishes
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Something is wrong with my compiler
PostPosted: Fri Jul 04, 2008 10:35 am     Reply with quote

Actually something is not well with my compiler. when I simulate the following piece of code I got my i counting down from 31-30-29----0 then to 255-254---0-.
What is wrong here?
When I get rid of i>=0 (replaced this with i > 0) then It counts down from 31,30,29---0 then to 31-30.. which correct.
I don't really understand why this happening with i>=0; i--
Code:

 temp = signal.new_ADC;  //adc1;//signal.adc;MCBSP_read(hMcbsp);
      // Update array samples
      for(i = 31; i >= 0; i--)
       {
         signal.Ring[i+1] = signal.Ring[i];
       }
      
      signal.Ring[0] = temp;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 04, 2008 12:10 pm     Reply with quote

If you don't understand something about the operation of a loop, then
make a test program and study it. Get rid of all your filter code and
only study the loop behavior. Example:
Code:
#include <16F877.H>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay (clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=================================
void main()
{
int8 i;

for(i = 31; i >= 0; i--)
   {
    printf("%u ", i);
   }


while(1);
}


Here's the output. It's easy to see that you have an undesired behavior.
Quote:

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
7 6 5 4 3 2 1 0 255 254 253 252 251 250 249 248 247 246 245 244


Look at what your test statement says in the "for loop". It says
"continue in this loop while 'i' is greater than or equal to 0.
Well, an int8 variable is an unsigned 8-bit variable. It can take a value
from 0 to 255. It's always "greater than or equal to" 0.

What change could you make to the declaration of 'i' to fix the problem ?
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Thanks you PCM Programmer and Tlemah
PostPosted: Fri Jul 04, 2008 9:39 pm     Reply with quote

Indeed it needs to be declared as signed int8 i;
Secondly Tlemah did you mean I do something like the following?

Code:
signal.Ring[tail] =  signal.new_ADC;
filter=&h1[order - tail];

for (i = 0; i < ORDER-1; i++)       
      {
             y += *(filter+ (sizeof(signed int16))) * Ring[i];
      }


with this, the (*filter++) values ,from the simulator, are 301,300,299,298,....207,301,300. These are not the values in the h1[]. My Compiler version 4.033.
What is happening here?
Ttelmah
Guest







PostPosted: Sat Jul 05, 2008 2:21 am     Reply with quote

The version 4 compilers should all do this correctly. Only the V3 compilers had the wrong pointer behaviour, and would need that fix.

4.033, ought to be 'OKish' in general. It is'borderline' on the point where the V4 compiler started to work reasonably.

As a 'comment', I presume you have got '#case'. Otherwise the compiler defaults to being case insensitive, and since you are relying on case to distinguish two versions of 'order', you will then get really screwy results...

Best Wishes
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Yeah got it right
PostPosted: Sat Jul 05, 2008 6:56 am     Reply with quote

Thank you guys, everything is working correctly. I just wonder how I can determine the number of cycles taken by my filter operation or rather the number of cycles taken in the for loop from the MPLAB simulator.

Are pointers faster than the arrays in this compiler.
Ttelmah
Guest







PostPosted: Sat Jul 05, 2008 2:59 pm     Reply with quote

Stopwatch option in the simulator.
Not really. On some chips, there is a really big gain. However there is a _small_ saving, by using the pointer, because it only has to add two on each loop, while using an array with index, it has to fetch the index, and multiply this by two.
You can do some more powerful savings, using a little assembler. The compiler only normally uses one of the table address pointers, and using assembler, you can use another. If you operate it in automatic increment mode, you can read the two bytes on each loop, and the hardware will automatically increment the address each time, and leave it ready for the next loop. A significant improvement.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group