DSPRelated.com
Forums

Re: [Fwd: Re: [c54x] code working on simulator but not on device?]

Started by megha daga May 22, 2006
Thanks Jeff
I have forwarded this messag eon matlab group too.
Jeff I was testing my result at matlab with result at DSP and I am not getting same outputs. If you get something from below calculations, Kindly reply.
Following are the calculations:
Fs = 48000;
>> HalfFs = Fs/2;
>> Wp = 6000/HalfFs;
>> Ws = 8600/HalfFs;
>> Rp = 3;
>> Rs = 20;
>> [n,Wn] = buttord(Wp,Ws,Rp,Rs)

n
6

Wn
0.2587

>> [b,a] = butter(n,Wn);
>> [sos,g] = tf2sos(b,a)

sos
1.0000 2.0085 1.0085 1.0000 -0.8084 0.1756
1.0000 2.0000 1.0000 1.0000 -0.9087 0.3215
1.0000 1.9915 0.9915 1.0000 -1.1577 0.6836

g
0.0012

x
-1233
424
-933
-933
-1233
-1984
1009
-225
1766
-140
-333
1416
102
-1217
704
1383
-1966

>> R = filter(b,a,x)

R
-1.5358
-13.1021
-52.7019
-135.9341
-260.9256
-414.7120
-591.4483
-783.6995
-942.4701
-962.3276
-742.9542
-291.2615
241.9823
647.3840
786.9343
655.2580
360.7719

Now calculations with normalized vector. I am using a factor of 3 as thats the highest number which can be with 2 bits.
>> sos = sos/3

sos
0.3333 0.6695 0.3362 0.3333 -0.2695 0.0585
0.3333 0.6667 0.3333 0.3333 -0.3029 0.1072
0.3333 0.6638 0.3305 0.3333 -0.3859 0.2279

>> sos = sos*32768

sos
1.0e+004 *

1.0923 2.1938 1.1016 1.0923 -0.8829 0.1918
1.0923 2.1845 1.0923 1.0923 -0.9926 0.3512
1.0923 2.1753 1.0830 1.0923 -1.2645 0.7467

[b,a] = sos2tf(sosnew,g)

R = filter(b,a,x)

R
-1.5358
-13.1021
-52.7019
-135.9341
-260.9256
-414.7120
-591.4483
-783.6995
-942.4701
-962.3276
-742.9542
-291.2615
241.9823
647.3840
786.9343
655.2580
360.7719

So its the same as before.
Now I need to check with the output I am getting at the DSP. In DSP I am using the iircas51 function and in taht they multiply the a1 coefficient of the biquad by a factor of 2. Now sos is nothing but:

sos = [b01, b11, b21, 1 , a11, a21
b02, b12, b22, 1, a12, a22
: : : : : :
b0L, b1L, b2L, 1, a1L, a2L]

for biquads. In DSP I need to pass 5 coefficients for each biquads, and hence for each biquad I am passing b0, b1, b2 , a1 and a2. Now as the DSP does calculation with a1 multiplied by 2, i am simply multiplying the a1 in sos by 2 and using that for filter calculations. I have checked this method and its working with other calculations (calculations where no quantization was there). So now

sos(1,5) = sos(1,5)*2
sos(2,5) = sos(2,5)*2
sos(3,5) = sos(3,5)*2

sos
1.0e+004 *

1.0923 2.1938 1.1016 1.0923 -1.7659 0.1918
1.0923 2.1845 1.0923 1.0923 -1.9851 0.3512
1.0923 2.1753 1.0830 1.0923 -2.5291 0.7467

g is same as before 0.0012. Didnt change its value.

>> [b,a] = sos2tf(sos,g)

b
1.0e+010 *

0.1623 0.9739 2.4348 3.2464 2.4348 0.9739 0.1623

a
1.0e+013 *

0.1303 -0.7492 1.5729 -1.4518 0.5665 -0.0918 0.0050

>> R = filter(b,a,x)

R
1.0e+007 *

-0.0000
-0.0000
-0.0000
-0.0000
-0.0001
-0.0004
-0.0012
-0.0029
-0.0070
-0.0162
-0.0362
-0.0790
-0.1693
-0.3573
-0.7445
-1.5361
-3.1435

And if I run that on the DSP, I am getting following output:
-46
-347
-1216
-2740
-4785
-7423
-10612
-13227
-13343
-10014
-4438
1009
4733
6482
6491
5308

Hence i am not getting the correct output. But if sos is in following form
sos
1.0e+004 *

1.0923 2.1938 1.1016 3.2768 -1.7659 0.1918
1.0923 2.1845 1.0923 3.2768 -1.9851 0.3512
1.0923 2.1753 1.0830 3.2768 -2.5291 0.7467

instead of prev one. that is if the 4th col is 1 as in original sos matrix and if g=1, I get same values as I am getting from DSP. That is the output is same.
I hope I am clear. Hence I am wondering am I doing normalization wrong or am I passing wrong values to the DSP?
Kindly tell me where am I going wrong.
Kindly reply
Thanks
Megha

Jeff Brower wrote: Megha-

Please post these questions to MATLAB group (m...).
They can help you figure out why your MATLAB code below is not yet
working.

-Jeff

> I did normalize and quantize the sos to Q15. But the thing I am not
> getting any difference in the filtered output of non quantized and
> quantized coefficients. I checked the pole zero plot in both the case and
> its the same.
> Following are my calculations:
>
> Fs = 48000;
> HalfFs = Fs/2;
> Wp = 6000/HalfFs;
> Ws = 8600/HalfFs;
> Rp = 3;
> Rs = 20;
> [n,Wn] = buttord(Wp,Ws,Rp,Rs)
>
> n >
> 6
> Wn >
> 0.2587
>
> [b,a] = butter(n,Wn);
>
> b >
> 0.0012 0.0075 0.0187 0.0249 0.0187 0.0075 0.0012
> a >
> 1.0000 -2.8748 3.9031 -3.0192 1.3840 -0.3521 0.0386
>
> sos >
> 1.0000 2.0085 1.0085 1.0000 -0.8084 0.1756
> 1.0000 2.0000 1.0000 1.0000 -0.9087 0.3215
> 1.0000 1.9915 0.9915 1.0000 -1.1577 0.6836
> g >
> 0.0012
> With above values and i/p as
> X = [100, 200, 150, 345, 23, 678, 23, 567, 890, 46]
> my output came :
> R = filter(b,a,X)
>
> R >
> Columns 1 through 8
>
> 0.1246 1.3546 6.9579 22.8703 54.7673 103.0378
> 160.8801 218.3556
>
> Columns 9 through 10
>
> 268.7207 312.9030
> Now I divided whole sos by 2.0085 (normalize) and multiplied by 32768
> (for
> > Q15) and then cal a and b:
> sosnew=sos/2.0085
>
> sosnew >
> 0.4979 1.0000 0.5021 0.4979 -0.4025 0.0874
> 0.4979 0.9958 0.4979 0.4979 -0.4524 0.1601
> 0.4979 0.9915 0.4937 0.4979 -0.5764 0.3404
>
> sosnew = sosnew*32768
>
> sosnew >
> 1.0e+004 *
>
> 1.6315 3.2768 1.6454 1.6315 -1.3188 0.2864
> 1.6315 3.2629 1.6315 1.6315 -1.4826 0.5246
> 1.6315 3.2491 1.6177 1.6315 -1.8888 1.1153
>
> [b,a] = sos2tf(sosnew,g)
> b >
> 1.0e+011 *
>
> 0.0541 0.3245 0.8113 1.0818 0.8113 0.3245
> 0.0541
>
> a >
> 1.0e+013 *
>
> 0.4342 -1.2484 1.6949 -1.3111 0.6010 -0.1529
> 0.0168
>
> and calculated filter on same data with these values:
> R = filter(b,a,X)
>
> R >
> Columns 1 through 8
>
> 0.1246 1.3546 6.9579 22.8703 54.7673 103.0378
> 160.8801 218.3556
>
> Columns 9 through 10
>
> 268.7207 312.9030
>
> So now you can see that, both the times R value is same. I dont
> understand why the values are not changing. Kindly provide your opinion.
> Kinldy reply.
> Thanking You
> Megha Daga
> Jeff Brower wrote: Megha-
>
> After normalizing and changing to Q15, then print sos again. What does it
> look like? If looks correct, then maybe sos2tf() does a normalization of
> its own, and puts the values back again. In that case, maybe you have to
> normalize and quantize b and a matrices.
>
> -Jeff
>
>> i just need to confirm what I am doing:
>> As you suggested to normalize the data and then quantize. I tested it on
>> matlab in the following manner:
>> initially sos was:
>> sos >>
>> 1.0000 2.0085 1.0085 1.0000 -0.8084 0.1756
>> 1.0000 2.0000 1.0000 1.0000 -0.9087 0.3215
>> 1.0000 1.9915 0.9915 1.0000 -1.1577 0.6836
>> g >>
>> 0.0012
>> With above values and i/p as
>> X = [100, 200, 150, 345, 23, 678, 23, 567, 890, 46]
>> my output came :
>> R = filter(b,a,X)
>>
>> R >>
>> Columns 1 through 8
>>
>> 0.1246 1.3546 6.9579 22.8703 54.7673 103.0378 160.8801
>> 218.3556
>>
>> Columns 9 through 10
>>
>> 268.7207 312.9030
>> Now I divided whole sos by 2.0085 (normalize) and multiplied by 32768
>> (for
>> Q15) and then cal a and b:
>> [b,a] = sos2tf(sosnew,g)
>> b >>
>> 1.0e+011 *
>>
>> 0.0541 0.3245 0.8113 1.0818 0.8113 0.3245 0.0541
>> a >>
>> 1.0e+013 *
>>
>> 0.4342 -1.2484 1.6949 -1.3111 0.6010 -0.1529 0.0168
>>
>> and calculated filter on same data with these values:
>> R = filter(b,a,X)
>>
>> R >>
>> Columns 1 through 8
>>
>> 0.1246 1.3546 6.9579 22.8703 54.7673 103.0378 160.8801
>> 218.3556
>>
>> Columns 9 through 10
>>
>> 268.7207 312.9030
>> and the result is same. Hence I dont see any effect by normalizing and
>> then quantizing.
>> I think this method should work.
>> Jeff kindly correct me if I am going wrong anywhere. I also checked a
>> random data on TO device and MATLAB and both give same output. Hence I
>> guess, if this quantizing thing works on matlab, it should work on
>> C5416.
>> Kindly provide your suggestion. My filter is an IIR low pass filter with
>> cut off at 6000Hz (for speech signal).
>> Thanking You
>> Megha Daga
>>
>> Jeff Brower wrote: Megha-
>> wont the scaling of coefficients effect the filter response?
>> Scaling all coefficients changes the overall filter gain, but the not
>> the
>> shape of the frequency response.
>> -Jeff
>> Jeff Brower wrote: Megha-
>> Thanks for the reply. But I guess I need the format in Q15 to use it
>> in
>> the command iircas51. Even if I convert it to Q13, it wont work.
>> Can you suggest a method so that I can quantize/normalize my whole data
>> between 1 and -1. After that I can convert it into Q15 and I guess that
>> should work.
>> iircas51() uses 5 coefficients per biquad, so it makes no assumption
>> that
>> one coefficient = 1 (see iircas4 function). In that case, you can try
>> dividing all coefficients by the magnitude of your largest coefficient.
>> Call this a scale factor k. Then apply 1/k to final output before it
>> goes
>> to the AIC to allow correct overall gain for filter output. Note that
>> you
>> should *not* apply 1/k to y[n] values -- only to D/A output, which I
>> think
>> can be more than 16 bits on the DSK 5510 board.Hopefully your IIR filter
>> can then work in Q15 without overflow. If not, then trying increasing
>> k.-Jeff
>> Jeff Brower wrote: Megha-> I had a query in Q
>> format. I am using iircas51 for IIR filter. In that
>>> the coefficient data type should be DATA thats defined as short. So my
>>> query is its not necessary to use Q15 format. I can aswell use Q2.13
>>> format. But jeff is there any function to convert float to Q2.13 as
>>> there is for float to Q15.To convert floating-point coefficients to
>>> Q13,
>>> multiply by 8192 (instead
>> of 32768 for Q15).I.e. multiplier = 2 ^^ QThis assumes all of your
>> coefficients are less than +/- 4.0-Jeff> Jeff Brower wrote: Megha- I had
>> one question.
>>> In functions like butter (for filters) we declare a variable Wn (the
>>> cut
>>> off frequency). ex: [B,A] = BUTTER(N,Wn). In this Wn value is between 0
>>> and 1, where 1 means half of sampling rate. What I am not understanding
>>> is
>>> where is it getting sample rate value from. I saw some codes and in
>>> that
>>> the sample rate value is not declared before using the command.
>>> Directly
>>> the command is used. Where is it getting sample rate value from?
>>> What I understand is suppose I have to run that filter design on an
>>> AIC.
>>> And AIC is set at some sampling rate ex 48KHz. Then the code is
>>> understanding that sample rate and setting Wn accordingly. Kindly
>>> correct
>>> me if I am wrong.
>>> If your actual sampling rate is 48 kHz, and you want lowpass
>>> Butterworth
>>> filter with 2 kHz cutoff, then set Wn = 0.0833.
>>> Digital filters don't "know" the actual analog sampling rate; they only
>>> know the Z-plane unit circle, or 0 to 1 as you mention. In this
>>> example,
>>> if you vary the sampling rate (reprogram the AIC) then the filter
>>> cutoff
>>> is always 0.0833*fs/2.
>>> -Jeff
>>>
>>> Jeff Brower wrote: Megha-
>>> I tried getting more material on maxflat but I am not getting much. I
>>> am
>>> not understanding what exactly SOS and G stand for.
>>> If I want coeff for IIRCAS51 (cascaded IIR filter design with biquads
>>> qith
>>> 5 coefficients) I want 3 coeff for zero (numerator) and 2 for poles
>>> (denominator). I am not getting how to get those. i am not
>>> understanding
>>> what does what stand for.
>>> Maxflat() is for Butterworth IIR filter design. It's one option -- you
>>> also might want Chebyshev or elliptic design.Another MATLAB function
>>> for
>>> converting direct-form transfer function to cascade of biquad sections
>>> is
>>> tf2sos(). Here are some pages that might give you some clues:
>>> http://www.math.psu.edu/local_doc/matlab/toolbox/signal/tf2sos.html
>>> http://www.ee.ic.ac.uk/pcheung/teaching/ee3_Study_Project/iir_lab2.pdf-Jeff
>>> Jeff Brower wrote: Megha-
>>> > This is in continuation to my previous mail. I also tried with a
>>> breakpoint at my
>>>> next statement after fltoq15. If I run that. It stops in between and
>>>> gives the
>>>> error:
>>>> Trouble running Target CPU: Attempted write to ROM at Addr:0x00ffff,
>>>> Page:0
>>>> I guess it is because of the infinite loop thing. Kindly reply and
>>>> suggest
>>>> something.Waiting for your reply.Your code is trying to write somwhere
>>>> in mem locations 0x8000-0xffff, which are
>>> external Flash as you mentioned before. Writing directly to Flash area
>>> won't work
>>> (although there is a way, which involves a series of Flash
>>> chip-specific
>>> commands).
>>> You have to check the C code ptr and array address values just before
>>> the
>>> breakpoint
>>> to see why they are not pointing at onchip or offchip SRAM.Also, did
>>> you
>>> see the c55x group thread about IIR cascade fixed-point filters? I've
>>> included a copy of the latest message below. If you're not subscribed
>>> to
>>> the c55x
>>> group, you should. C55xx devices are the next generation after C54xx --
>>> both 16-bit
>>> fixed-point.-Jeff
>>> -------- Original Message --------
>>> Subject: Re: [c55x] IIR filter coefficients in iircas5 dsplib
>>> Date: Tue, 16 May 2006 08:37:25 +0200
>>> From: Christian Narvaez
>>> To: c... thanks Misan, thanks RK:
>>> indeed this was the problem... I have successfully implemented the
>>> filter now.CNnasim ahmed ha scritto:
>>>> hi christian,
>>>> u r trying to get q14 fixed point format but dsplib
>>>> has q15 format, i suggest u to use both in same format . u can
>>>> multiply with 2^15 . And two q15 multiplication results q2.30. here 2
>>>> sign bit presents so u hav to modify that according to ur desired
>>>> output format. i hope it will help u , if u hav any prob u can send me
>>>> message.
>>>>
>>>> regards misan
>>>>
>>>> */Christian Narvaez /* wrote:
>>>>
>>>> Hi,
>>>> I am having a problem trying to implement an IIR filter on an TI c55x.
>>>> I have calculated the filter on matlab getting the following
>>>> coefficients for 2 second order sections:
>>>>
>>>> SOS
>>>> 1.0000 1.6854 1.0000 1.0000 -1.7197 0.8565
>>>> 1.0000 -1.9999 1.0000 1.0000 -1.8899 0.9250
>>>> G
>>>> 1.0000
>>>> 0.1041
>>>> 1.0000
>>>>
>>>> Simulations perform as expected.
>>>> Since the coefficients are between ]-2..2[ I transform the SOS matrix
>>>> as SOS_fix = SOS*2^14:
>>>>
>>>> SOS_fix
>>>> 16384 27614 16384 16384 -28175 14033
>>>> 16384 -32766 16384 16384 -30964 15155
>>>>
>>>> The question is: can I run this coefficients on iircas5 of the TI
>>>> dsplib? I know dsplib is Q.15 while my coefficients are Q.14, but I
>>>> suppose this is not a problem if the filter's input is small
>>>> enough not
>>>> to cause any overflow. Unfortunately this does not work, the
>>>> output is
>>>> completely wrong.
>>>>
>>>> The second think I tried was to multiply the numerator
>>>> coefficients of
>>>> the second section of the filter by 0.1041 (according to G), before
>>>> converting them to Q.14, getting this:
>>>>
>>>> SOS_fix_G
>>>> 16384 27614 16384 16384 -28175 14033
>>>> 1706 -3412 1706 16384 -30964 15155
>>>>
>>>> But this also does not work.
>>>>
>>>> Does anyone have any idea how I should convert my coefficients in
>>>> order
>>>> to work with iircas5?
>>>>
>>>> Thanks.
>>>>
>>>> Christian

---------------------------------
Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.