This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: I can't get the floats to work, I guess...

Part Number: TM4C123GH6PM

Hello,

First of all, I am thoroughly confused. Floats work perfectly fine in other modules, but in the main function, after declaring the float32_t variable (tried with just float), I can't even assign a 1.0f to that variable. I'm using keil, and FPU is enabled. This one and that other module, where floats work, have same relevant "includes". And I've checked that they work over there!

I'm using the debugger, which tells me that variable is set to 0. 

Any advice is appreciated. 

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "driverlib/sysctl.h"
#include "settings.h"
#include "sampler.h"
#include "arm_math.h"
#include "forward.h"

#include "mfcc.h"
#include "arm_const_structs.h"

//int16_t data[7168];  //TEST: SOUND
uint32_t n_prev=0;		//TEST: SOUND
extern float32_t signal[1024];
extern float32_t probability;
extern float32_t alphas_prev_f32[4];
static float32_t prob;

int main(void)
{
	//uint32_t i = 0;			//TEST: SOUND
	//char num[6];				//TEST: SOUND
	int i = 0;
	int j, k;
	float32_t features[MFCC_SIZE];
	float32_t powers[WORD_LENGTH];
	float32_t observations[WORD_LENGTH][MFCC_SIZE];
	float32_t halved_frame[256];		// Place to store (ADC_OUT - 2047) * 3.3 / 4095
	float32_t halved_frame_prev[256]; // Place for above's previous value, after it's been updated
	float32_t input_f32_16khz[1024];
	uint8_t skip = 1;
	float32_t power_mean;
	
	
	float32_t start_power = 0.1f;			// BUG
  float32_t end_power = 0.1f;				// BUG
	uint8_t cnt1, cnt2;
	
	uint16_t count = 0;
	
	init_peripherals();
	init_forward();
	

	
	while(1){
		
		if(n == n_prev)		// Wait until there is a new sample batch ready
			continue;
		// Following is due to PING_PONG DMA
		if(n%2 == 1){
			for(j = 0; j < 256; j++){
				halved_frame[j] = (ADC_OUT_0[j] - 2047) * 8.058608e-4;
			}
		}
		else {
			for(j = 0; j < 256; j++){
				halved_frame[j] = (ADC_OUT_1[j] - 2047) * 8.058608e-4;
			}
		}
		
		arm_fill_f32(0, input_f32_16khz, BUFSIZE); // Initialize the input array to 0s
		// The 'input_f32_16khz' is a complex array, and because its values are real,
		// every second element is 0. It's were we store the frame
		// 'halved_frame_prev' covers the first half of the array (256) samples, and
		// 'halved_frame' covers the second half (256 + 256 = 512 samples)
		for(j = 0; j < 256; j++){
			input_f32_16khz[j * 2] = halved_frame_prev[j];
			input_f32_16khz[512 + j * 2] = halved_frame[j];
		}
		memcpy(halved_frame_prev, halved_frame, 256 * sizeof(*halved_frame));
		
		get_MFCC(input_f32_16khz, features, powers + i);
		//memcpy(observations[i], features, 12 * sizeof(*features));
		arm_copy_f32(features, observations[i], 12);
		
//		if(features[0] > 40.0)
//			n = n;
		i = (i + 1) % WORD_LENGTH;
		
		arm_copy_f32(start_prob, alphas_prev_f32, 4);
		probability = 0;
		
		cnt1=0;
		cnt2=0;
		start_power = 0.1f;		//BUG
		end_power = 0.1f;			//BUG
		for(j = i; j < WORD_LENGTH; j++){
			if(skip){
				skip = 0;
				continue;
			}
			if(cnt1<4){
				start_power = (float32_t)(start_power + (powers[j] * powers[j])); //BUG
				cnt1++;}
			else if(cnt2 < 4){
				end_power += (float32_t)(powers[j] * powers[j]);   //BUG
				cnt2++;}
			emission(observations[j]);
			forward_step();
		}
		for(k = 0; k < i; k++){
			if(skip){
				skip = 0;
				continue;
			}
			if(cnt1<4){
				start_power += powers[k] * powers[k];			//BUG
				cnt1++;}
			else if(cnt2 < 4){
				end_power += powers[k] * powers[k];				//BUG
				cnt2++;}
			emission(observations[k]);
			forward_step();
		}
		skip = 1;   // it's used to skip the first coefficient
		
		
		prob = probability;
		
		if(prob < 600.0f && start_power > 0.35f && end_power > 0.35f)
			count++;
		n_prev = n;
	}
	while(1){}
	
}

  • Hello Savo,

    This sounds like a compiler or C code related issue, so not entirely sure I can help, but to even try I'll need to see the code itself in order to make any suggestions.

    Also would be helpful to know of any errors or warnings you are getting? And how are you checking that the float isn't working in main?
  • Hey,

    I have included the code, and have put the "BUG" in the comments where the dubious code is. No warnings that would relate to this. No errors, either.

    Thanks.
  • Hello Savo,

    Hmm... I'm not vastly experienced with floating points, but whenever I used float's, I just had it declared as value = 3.1416; without anything else. From my experience, just including the decimal point is all that is needed, at least for CCS.

    I read about how the 'f' can help though, as the compiler should make it a double by default before assigning it to float, and then you'd get inaccuracy by not using it. However, that seems to be compiler specific.

    I'd suggest testing a few things...
    1) Try assigning values like 0.2, 0.6, 1.1, 1.6 etc and see if there is any rounding and/or truncation occurring
    2) Try removing the 'f' from the assignment and see if Keil prefers that
    3) Try using double over float and see if there is any change in behavior

    Honestly this isn't really a TM4C topic, so you might have better luck trying to get help from Keil or even on something like stackoverflow. I could easily be missing something super obvious because I just lack experience with the topic...
  • Well, I have tried all 3 recommendations, but no success. Thanks anyway!

    Yeah, I don't really know even how to phrase these questions. Syntax is correct, without a doubt. In one .c file it works fine, in other it doesn't. Weird...