watching variable values in Code Composer - c

I work in Code Composer Studio Version: 6.0.1.00040
with the card LCDK C6748, but I think this is a more general question, relating to CCS generally.
I'm trying to implement LMS for cancelling acoustic echoes,
this is the skeleton of my .c file:
void waitForInterrupt()
{
while (flag==0) {}
flag=0; // reach this line only when flag == 1
}
interrupt void interrupt4(void)
{
// Inputs
inputRight_micSignal = (float)input_right_sample();
// Outputs
outputLeft_referenceSignal= whiteNoiseSample;
codec_data.channel[RIGHT]= (uint16_t)outputRight_cleanedSound;
codec_data.channel[LEFT]= (uint16_t)outputLeft_referenceSignal;
output_sample(codec_data.uint);
flag = 1;
}
void main()
{
// variables decelerations
int i;
float filter_output;
// initialising filter coefficients
for (i=0 ; i<ADAPTIVE_FILT_SIZE ; i++) // initialise weights and delay line
{
w[i] = 0.0;
x[i] = 0.0;
}
// initialising the interrupt routine
L138_initialise_intr(FS_8000_HZ,ADC_GAIN_0DB,DAC_ATTEN_0DB,LCDK_MIC_INPUT);
while(1) // adaptive filtering routine
{
waitForInterrupt();
whiteNoiseSample = getPrnFiltered();
for (i = ADAPTIVE_FILT_SIZE-1; i > 0; i--) // update delay line - TDL:merge later with w loop (still make sure no magic NaN's appear)
{
x[i] = x[i-1];
}
x[0] = outputLeft_referenceSignal; // input to adaptive filter
filter_output = 0; //reseting filter output
// compute adaptive filter output
for (i = 0; i < ADAPTIVE_FILT_SIZE; i++)
filter_output += (w[i]*x[i]);
outputRight_cleanedSound = inputRight_micSignal - filter_output; // compute error
for (i = ADAPTIVE_FILT_SIZE-1; i >= 0; i--) // update weights and delay line
{
w[i] = w[i] + beta*outputRight_cleanedSound*x[i]; // w[i]+=beta*"error"*"reference"
}
from some reason when I put the arrays x[] and w[] in the "watch table"
and I suspend the running of the program (in order to examine w[] coefficients after awhile, I see that it is full of NaN's - while x[] contains "regular"
values.
when I put breakpoint inside the line where w[] is calculated:
w[i] = w[i] + beta*outputRight_cleanedSound*x[i]; // w[i]+=beta*"error"*"reference"
I see the flow goes there.
What could be the reason for the NaN's?
Is there a way to watch w[] in the "wach table"?

These three steps work for me:
1) First you need to make sure the variables are globally available (e.g. that they are not allocated on the stack).
2) You need to halt the processor before trying to read the variables. (In Debug view: Tools -> Debugger Options -> Auto Run and Launch Options).
3) Enable "halt the target before any debugger access" on the watch window and click the "auto-update" icon in the "Variables"-window.
I've uploaded a screenshot with red boxes around the stuff you need to touch.
See if that helps you :) Otherwise check out TI's Engineer2Engineer forum (E2E). In my experience the TI guys are quick to answer and I've gotten very competent help from them.
Tell me how it works for you :) ?
FWIW I'm using Code Composer Studio v.5.5.0.00077.

Related

Animated ASCII art flickering in c

I'm making an ASCII art game in c (windows), but for some reason when i play it is all flickering at apparently random intervals, and for most of the time i can't see anything. can anyone explain why or how to solve it?
this is my code:
const int WIDTH = 20, HEIGTH = 5;
const int AREA = (WIDTH) * HEIGTH;
const int gl = HEIGTH - 2; //Ground level
const float delta = 0.1f; //Frame rate
char scr[AREA]; //String for displaying stuff
char input;
int posx = 10, posy = gl - 1; //Player position
int vel = 0; //player velocity
while(1)
{
//TODO: player input
for(i = 0; i < AREA; i++) //Rendering part
{
if(i % WIDTH == 0 && i != 0)
{
scr[i] = '\n';
continue;
}
if(floor(i / WIDTH) >= gl) //i is on ground
{
scr[i] = '.';
continue;
}
scr[i] = ' ';
}
//Set player position
scr[posy * WIDTH + posx + 1] = '#';
scr[(posy + 1) * WIDTH + posx + 1] = '#';
system("cls");// Clear terminal
printf(scr);// Print screen
usleep(delta * 1000);//Sleep
}
output:
#
..........#........
...................►↓#
It works, but it flickers...
One possible reason for your problem is that there may be output waiting in the output buffer when you call your sleep function usleep. In that case, you should always flush all output before sleeping, by calling the function fflush( stdout );.
Also, using system("cls"); is highly inefficient. It is generally better to use the Console Functions API, or, if you are targetting platforms with Windows 10 or later, you can also use Console Virtual Terminal Sequences instead.
If you decide to use the Console Functions API, then the main functions that you will be needing in order to replace system("cls"); are GetStdHandle to obtain the output handle, and SetConsoleCursorPosition. That way, you won't have to clear the whole screen, but will only have to overwrite the parts of the screen that are changing. I also recommend that you replace printf with WriteConsole, otherwise you may run into buffering issues.
If this does not solve your flickering problem, then there also is the possibility of using double-buffering. Both the Console Functions API and Console Virtual Terminal Sequences support this.
When using the Console Functions API, you can use the function CreateConsoleScreenBuffer to create a new buffer to write to, without it being displayed immediately. This will allow you to first finish building the next screen, without any flickering occuring while doing so (because it is not being displayed yet). Once you have finished building the next screen, you can display it using the function SetConsoleActiveScreenBuffer.
Console Virtual Terminal Sequences offer similar functionality by supporting an alternate screen buffer.

Valgrind Errors - Conditional Jump or move depends on uninitialised values

img of error
Above is an error I have been getting, in relation to this line in my program:
storedData[k] = min(dist[index1][k],dist[index2][k]);
Now here is the surrounding functions to this line:
for(int k = 0; k <arraySize; k++){
if(k!= index1 && k != index2){
if(method == SINGLE_LINKAGE){
storedData[k] = min(dist[index1][k],dist[index2][k]);
} else {
storedData[k] = max(dist[index1][k],dist[index2][k]);
}
}
}
Now after playing around with it for quite a while, I realised that the issue it has is with the incrementing 'k' variable in the for loop. More specifically it is worried that when used as an index in dist, the value returned will be uninitialised. Now in terms of functionality, my program works fine and does everything I want it to do. More notably, I have also initialised this function elsewhere in a helper function which is why this confuses me more. I have initialised all the values from index 0-arraysize which in my head means this should never be an issue. Im not sure if maybe this is caused because its done outside of the main function or something. Regardless it keeps giving me grief and I would like to fix it.
You need to work back from the error to its origin. Even if you are initializing your arrays, it is possible that something is 'uninitializing' them again afterwards. memcheck does not flag uninitialized data when it is copied, only when it affects the outcome.
So in pseudo-code you might have
Array arr;
Scalar uninit; // never initialized
init_array(arr);
// do some stuff
arr[3] = uninit; // no error here
for (i = 1 to arr.size)
store[i] = max(arr[i], arr[i-1]; // errors for i == 3 and 4
There are two things that you could try. Firstly, try some 'printf' debugging, something like
for(int k = 0; k <arraySize; k++) {
if(k!= index1 && k != index2) {
fprintf(stderr, "DEBUG: k %d index1 %d index2 %d\n", k, index1, index2);
// as before
Then run Valgrind without a log file. You should then be able to see which indices cause the error(s).
Next, try using ggbserver. Run valgrind in one terminal with
valgrind --vgdb-error=0 prog args
and then run gdb in a second terminal to attach (see the text that is output in the 1st terminal for the commands to use).
You can then use gdb as usual (except no 'run') to control your guest app, with the additional ability to run valgrind monitor commands.

Xcode Seemingly Unable to Use Arrays in C

It's very likely that this is user error as I'm somewhat new to Xcode, but I can't figure this out. Hopefully one of you can.
I'm trying to add an array using the following code:
int ArraySum (int MyArray [], int size) {
int sum = 0;
for (int i = 0; i< size; i++) {
sum = sum + MyArray [i];
}
return sum;
}
int main (){
int mynumberarray [6] = {1,2,3,4,5,6};
int the_sum = ArraySum (mynumberarray, 6);
printf ("The Sum is = %d \n", the_sum);
return 0;
}
When I click the build & run button in Xcode, the only output I get is (lldb).
This would typically lead me to believe that I made a mistake somewhere, but when I run the code through Terminal it runs perfectly and gives me the correct sum.
Can anyone help me here? This isn't an isolated incident, I've had several issues working with arrays in Xcode that are working perfectly when executed in Terminal.
Screenshot:
You have breakpoints set in your Xcode project, so the program is pausing at the first breakpoint and giving you the (lldb) debugger prompt. Disable the breakpoints and run and everything should behave as expected. You can either manually disable each breakpoint (by clicking on it - it should then become dimmed), or go to Debug => Disable Breakpoints in the menu bar.

Low Pass filter in C

I'm implementing a low pass filter in C wih the PortAudio library.
I record my microphone input with a script from PortAudio itself. There I added the following code:
float cutoff = 4000.0;
float filter(float cutofFreq){
float RC = 1.0/(cutofFreq * 2 * M_PI);
float dt = 1.0/SAMPLE_RATE;
float alpha = dt/(RC+dt);
return alpha;
}
float filteredArray[numSamples];
filteredArray[0] = data.recordedSamples[0];
for(i=1; i<numSamples; i++){
if(i%SAMPLE_RATE == 0){
cutoff = cutoff - 400;
}
data.recordedSamples[i] = data.recordedSamples[i-1] + (filter(cutoff)*(data.recordedSamples[i] - data.recordedSamples[i-1]));
}
When I run this script for 5 seconds it works. But when I try to run this for more then 5 seconds it fails. The application records everything, but crashes on playback. If I remove the filter, the application works.
Any advice?
The problem:
you are lowering the cutoff frequency by 400 Hz everytime i%SAMPLE_RATE == 0
never stop so you go below zero
this is not done once per second !!!
instead every time your for passes through second barrier in your data
that can occur more often then you think if you are not calling your calls in the right place
which is not seen in your code
you are filtering in wrong oorder
... a[i]=f(a[i],a[i-1]; i++;
that means you are filtering with already filtered a[i-1] value
What to do with it
check the code placement
it should be in some event like on packed done sompling
or in thread after some Sleep(...); (or inside timer)
change the cut off changing (handle edge cases)
reverse filter for direction
Something like this:
int i_done=0;
void on_some_timer()
{
cutoff-=400;
if (cutoff<1) cutoff=1; // here change 1 for limit frequency
if (numSamples!=i_done)
for (i=numSamples-1,i>=i_done;i--)
data.recordedSamples[i] = data.recordedSamples[i-1] + (filter(cutoff)*(data.recordedSamples[i] - data.recordedSamples[i-1]));
i_done=numSamples;
}
if your code is already OK (you did not post th whole thing so I can missing something)
then just add the if (cutoff<1) cutoff=1; after cutoff change

"Saving" current state in foor loop to continue later

currently I'm thinking about the following problem and maybe someone can help me:
For minecraft I want to change a lot of blocks and to prevent lags I want to change only a couple of blocks at the same time. To change a cuboid I usually use a loop like this:
for(int x=t.x; x<t.X; x++)
for(int y=t.y; y<t.Y; y++)
for(int z=t.z; z<t.Z; z++) {
// ..
}
where t saves the from and to coords.
Now I want to save the current progress to continue later.
Please help me im tired of thinking about it..
Your code looks like C. In C, a process cannot return to a given stack state after leaving the calling functions. So leaving a loop and later returning to it are not possible at the language level. In other languages, things are different. E.g. in the Pypy implementation of the Python language, continuelets can be used to achieve what you describe.
However, you can achieve similar ways by using your own objects to store the last counters.
struct counters { int x, y, z; };
bool continueLoops(struct counters *ctrs) {
for (; ctrs->x < t.X; ctrs->x++) {
for (; ctrs->y < t.Y; ctrs->y++) {
for (; ctrs->z < t.Z; ctrs->z++) {
// ..
if (weWantToInterruptTheLoop)
return true;
}
ctrs->z = t.z;
}
ctrs->y = t.y;
}
return false;
}
void startLoops() {
struct counters ctrs;
ctrs.x = t.x;
ctrs.y = t.y;
ctrs.z = t.z;
while (continueLoops(&ctrs)) {
// do whatever you want to do between loops
}
}
However, I don't see much benefit in the above approach, as opposed to perform the relevant operation directly within the inner loop. So I'm not sure whether this is useful for you.

Resources