Update char array with Int and float values, without sprintf? - c

I am using C to program a microcontroller (NHS3152) to calculate resistance using this formula:
RES = (V1-V2)/I
I want to write a program that:
Updates the values of 3 Floats and 1 integer (V1, V2, I, RES)
Update uint8_t text[] = "Char to store values of V_1, V_2, I, Res"; with a string with the values of these 3 floats and 1 integer
Commits text to memory using the function: "Example1_Creating_an_NDEF..." (provided below)
The issue I am having is with Updating text with the values of the 3 floats and 1 integer. I am able to do it with sprintf, however I cannot use this function with my microcontroller (I dont know why it stops me from flashing it to memory)
The code bellow is an example of what i want to achieve in from point 2 above, in the real code the float and integer values are updated by getting values from sensors on the mircocontroller:
#include <stdio.h>
#include <stdint.h>
static volatile float V_1 = 5.0; // store value of Voltage 1
static volatile float V_2 = 2.0; // store value of voltage 2
static volatile int I = 5; // store current value
static volatile float RES; // calcualte resitance
int i=1;
uint8_t text[] = "Char to store values of V_1, V_2, I, Res";
int main(void)
{
printf( text);
printf("\n");
//updates values for ADC_1, ADC_2, I, Res,
while(i<=2){ // real while loop is infinite
V_1 ++ ; // Usually value updates from microcontroller sensor
V_2 ++ ;
I ++;
RES = (V_1-V_2)/I; // calculating resistance
sprintf((char *)text, "Updated Text V1: %6.2f V2: %6.2f I: %8.d resistance: %e", V_1, V_2,I, RES ); // what i want to do but without sprintf
printf( text);
printf("\n");
i++;
}
printf("END");
}
OUT:
Char to store values of V_1, V_2, I, Res
Updated Text V1: 6.00 V2: 3.00 I: 6 resistance: 5.000000e-01
Updated Text V1: 7.00 V2: 4.00 I: 7 resistance: 4.285714e-01
END
Here is the Code including the function Example1_Creating_an_NDEF.. from point 3. This working code manages to Commit the text to memory (it works). All i need is to be able to update the text without sprintf as i believe these functions aren't allowed when i'm not in debug mode.
#include "board.h"
// From ndeft2t
#include "ndeft2t/ndeft2t.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
uint8_t instance[NDEFT2T_INSTANCE_SIZE] __attribute__((aligned (4)));
uint8_t buffer[NFC_SHARED_MEM_BYTE_SIZE ] __attribute__((aligned (4)));
uint8_t locale[] = "en";
uint8_t text[] = "Char to store values of V_1, V_2, I, Res";
// I want to add these to the text message
static volatile float V_1 = 5.0;
static volatile float V_2 = 2.0;
static volatile int I = 5;
static volatile float Res = ;
static void Example1_Creating_an_NDEF_Message_with_a_single_record_of_type_TEXT (void)
{
Chip_NFC_Init(NSS_NFC); /* Is normally already called during board initialization. */
NDEFT2T_Init();
NDEFT2T_CreateMessage(instance, buffer, NFC_SHARED_MEM_BYTE_SIZE, true);
NDEFT2T_CREATE_RECORD_INFO_T recordInfo = {.shortRecord = true, .pString = locale};
if (NDEFT2T_CreateTextRecord(instance, &recordInfo)) {
/* The payload length to pass excludes the NUL terminator. */
if (NDEFT2T_WriteRecordPayload(instance, text, sizeof(text) - 1)) {
NDEFT2T_CommitRecord(instance);
}
}
NDEFT2T_CommitMessage(instance);
/* The return value of the commit function is ignored here. */
}
int main(void)
{
Board_Init();
//NDEFT2T_Init();
/* Optional feature: send the ARM clock to PIO0_1 */
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_PIO0_1, IOCON_FUNC_1);
Chip_Clock_Clkout_SetClockSource(CLOCK_CLKOUTSOURCE_SYSTEM);
/* Blink & attach a message to Memory */
while (1) {
//updates values for V_1, V_2, I, Res,
V_1++;
V_2++;
I++;
RES = (V_1 - V_2)/RES;
// Update the text message to contain "(V_1, V_2, I, RES)"
LED_Toggle(LED_RED);
Example1_Creating_an_NDEF_Message_with_a_single_record_of_type_TEXT();
Chip_Clock_System_BusyWait_ms(500);
}
return 0;
}
EDIT: responding to KamilCuk's comment : Do you really need float? Does your mcu has floating point support? Consider using integers only.
The fucntions i use to get V1,V2 & I, all return an Integer (eg: V1=2931 12 bit converter so between 0 and 4096 ) however to convert the integer value to the real value i need to use the following conversion:
V1_real= (V1* 1.2) / 2825 + 0.09; // CONVERSION added line
Without conversion i Cannot calculate Res.
An acceptable compromise is to commit to memory the values V1, V2, I without converstion to real values. I can then calculate RES in a second momemnt, once i retrieve the data from the MCU ( i am using my phone as an NFC reader, the chip is an NFC tag)
So the question is:
How do i convert the message in text to a message containing the integers (V_1,V_2,I)? :
Something like:
uint8_t text[] = "Char to store values of V_1, V_2, I";
text = "Message with V_1: V_1value, V_2: V_2value, I: IValue
Below is the code i use to extract the value of V_1 & V_2:
void adc (void)
{
Chip_IOCON_SetPinConfig(NSS_IOCON, IOCON_ANA0_1, IOCON_FUNC_1);
Chip_ADCDAC_SetMuxADC(NSS_ADCDAC0, ADCDAC_IO_ANA0_1);
Chip_ADCDAC_SetInputRangeADC(NSS_ADCDAC0, ADCDAC_INPUTRANGE_WIDE);
Chip_ADCDAC_SetModeADC(NSS_ADCDAC0, ADCDAC_SINGLE_SHOT);
Chip_ADCDAC_StartADC(NSS_ADCDAC0);
//Getting the data
while (!(Chip_ADCDAC_ReadStatus(NSS_ADCDAC0) & ADCDAC_STATUS_ADC_DONE)) {
; /* Wait until measurement completes. For single-shot mode only! */
}
//Data V1 stored here
adcInput_1 = Chip_ADCDAC_GetValueADC(NSS_ADCDAC0);
// Usually i use this line to then conver it to the real value of V1
//adcInput_1= (adcInput_1 * 1.2) / 2825 + 0.09; // CONVERSION added line
//! [adcdac_nss_example_3]
}
Maybe someone has a better solution that allows me to actually arrive at having also RES calculated, knowing that:
RES= V1_real-V1_real/I_real
V1_real= (V1* 1.2) / 2825 + 0.09 and 0< V1 < 4000 (12 bit converter for V1)
V2_real= (V2* 1.2) / 2825 + 0.09 and 0< V2 < 4000
I_real = I*1e-12 0<I<4000

Related

How do I do decrements in C language?

If an example , I set a value of :
a = 50
How do I do a decrement whereby a always decreases by 2 ? An example :
48
46
44
42
40
Whereby the return results , I can use as a variable. Example in the case below , this method is to get temperature at random. But I would like it to decrease so that I can call this get temperature method even below for it to loop.
/* Retrieves current temperature. */
static int32_t get_temperature_sample(void)
{
/* For the sake of example, random data is used */
return rand() % 10 + 25;
}
/* Periodically called by Kaa SDK. */
static void example_callback(void *context)
{
time_t current_time = time(NULL);
/* Respect sample period */
if (difftime(current_time, last_sample_time) >= sample_period) {
int32_t temperature = get_temperature_sample();
printf("Sampled temperature: %i\n", temperature);
last_sample_time = current_time;
kaa_user_log_record_t *log_record = kaa_logging_data_collection_create();
log_record->temperature = temperature;
kaa_logging_add_record(kaa_client_get_context(context)->log_collector, log_record, NULL);
}
}
Like if the above codes , if I were to say that for my temperature. I wouldnt want it to be random as you can see it uses random. I want to set a value for my temperature and decrease like a constant until it reaches 0 in my print function shown below.
printf("Sampled temperature: %i\n", temperature);
last_sample_time = current_time;
Try something like this
static void get_temperature_sample(int *previous_temp)
{
*previous_temp -= 2; // This decrements the value and
// don't need a return value, because
// you are passing a pointer to
// the variable and hence modifying
// it's value directly
}
And when you call the function
int32_t temperature = 50; // Initial temperature
get_temperature_sample(&temperature);
You'll need to add a while somewhere in there depending on how you're tracking the temperature. But this will decrement the temp value whenever you call the function.
To decrement a, try using
a -=2;
it will help you..

C: fastest way to evaluate a function on a finite set of small integer values by using a lookup table?

I am currently working on a project where I would like to optimize some numerical computation in Python by calling C.
In short, I need to compute the value of y[i] = f(x[i]) for each element in an huge array x (typically has 10^9 entries or more). Here, x[i] is an integer between -10 and 10 and f is function that takes x[i] and returns a double. My issue is that f but it takes a very long time to evaluate in a way that is numerically stable.
To speed things up, I would like to just hard code all 2*10 + 1 possible values of f(x[i]) into constant array such as:
double table_of_values[] = {f(-10), ...., f(10)};
And then just evaluate f using a "lookup table" approach as follows:
for (i = 0; i < N; i++) {
y[i] = table_of_values[x[i] + 11]; //instead of y[i] = f(x[i])
}
Since I am not really well-versed at writing optimized code in C, I am wondering:
Specifically - since x is really large - I'm wondering if it's worth doing second-degree optimization when evaluating the loop (e.g. by sorting x beforehand, or by finding a smart way to deal with the negative indices (aside from just doing [x[i] + 10 + 1])?
Say x[i] were not between -10 and 10, but between -20 and 20. In this case, I could still use the same approach, but would need to hard code the lookup table manually. Is there a way to generate the look-up table dynamically in the code so that I make use of the same approach and allow for x[i] to belong to a variable range?
It's fairly easy to generate such a table with dynamic range values.
Here's a simple, single table method:
#include <malloc.h>
#define VARIABLE_USED(_sym) \
do { \
if (1) \
break; \
if (!! _sym) \
break; \
} while (0)
double *table_of_values;
int table_bias;
// use the smallest of these that can contain the values the x array may have
#if 0
typedef int xval_t;
#endif
#if 0
typedef short xval_t;
#endif
#if 1
typedef char xval_t;
#endif
#define XLEN (1 << 9)
xval_t *x;
// fslow -- your original function
double
fslow(int i)
{
return 1; // whatever
}
// ftablegen -- generate variable table
void
ftablegen(double (*f)(int),int lo,int hi)
{
int len;
table_bias = -lo;
len = hi - lo;
len += 1;
// NOTE: you can do free(table_of_values) when no longer needed
table_of_values = malloc(sizeof(double) * len);
for (int i = lo; i <= hi; ++i)
table_of_values[i + table_bias] = f(i);
}
// fcached -- retrieve cached table data
double
fcached(int i)
{
return table_of_values[i + table_bias];
}
// fripper -- access x and table arrays
void
fripper(xval_t *x)
{
double *tptr;
int bias;
double val;
// ensure these go into registers to prevent needless extra memory fetches
tptr = table_of_values;
bias = table_bias;
for (int i = 0; i < XLEN; ++i) {
val = tptr[x[i] + bias];
// do stuff with val
VARIABLE_USED(val);
}
}
int
main(void)
{
ftablegen(fslow,-10,10);
x = malloc(sizeof(xval_t) * XLEN);
fripper(x);
return 0;
}
Here's a slightly more complex way that allows many similar tables to be generated:
#include <malloc.h>
#define VARIABLE_USED(_sym) \
do { \
if (1) \
break; \
if (!! _sym) \
break; \
} while (0)
// use the smallest of these that can contain the values the x array may have
#if 0
typedef int xval_t;
#endif
#if 1
typedef short xval_t;
#endif
#if 0
typedef char xval_t;
#endif
#define XLEN (1 << 9)
xval_t *x;
struct table {
int tbl_lo; // lowest index
int tbl_hi; // highest index
int tbl_bias; // bias for index
double *tbl_data; // cached data
};
struct table ftable1;
struct table ftable2;
double
fslow(int i)
{
return 1; // whatever
}
double
f2(int i)
{
return 2; // whatever
}
// ftablegen -- generate variable table
void
ftablegen(double (*f)(int),int lo,int hi,struct table *tbl)
{
int len;
tbl->tbl_bias = -lo;
len = hi - lo;
len += 1;
// NOTE: you can do free tbl_data when no longer needed
tbl->tbl_data = malloc(sizeof(double) * len);
for (int i = lo; i <= hi; ++i)
tbl->tbl_data[i + tbl->tbl_bias] = fslow(i);
}
// fcached -- retrieve cached table data
double
fcached(struct table *tbl,int i)
{
return tbl->tbl_data[i + tbl->tbl_bias];
}
// fripper -- access x and table arrays
void
fripper(xval_t *x,struct table *tbl)
{
double *tptr;
int bias;
double val;
// ensure these go into registers to prevent needless extra memory fetches
tptr = tbl->tbl_data;
bias = tbl->tbl_bias;
for (int i = 0; i < XLEN; ++i) {
val = tptr[x[i] + bias];
// do stuff with val
VARIABLE_USED(val);
}
}
int
main(void)
{
x = malloc(sizeof(xval_t) * XLEN);
// NOTE: we could use 'char' for xval_t ...
ftablegen(fslow,-37,62,&ftable1);
fripper(x,&ftable1);
// ... but, this forces us to use a 'short' for xval_t
ftablegen(f2,-99,307,&ftable2);
return 0;
}
Notes:
fcached could/should be an inline function for speed. Notice that once the table is calculated once, fcached(x[i]) is quite fast. The index offset issue you mentioned [solved by the "bias"] is trivially small in calculation time.
While x may be a large array, the cached array for f() values is fairly small (e.g. -10 to 10). Even if it were (e.g.) -100 to 100, this is still about 200 elements. This small cached array will [probably] stay in the hardware memory cache, so access will remain quite fast.
Thus, sorting x to optimize H/W cache performance of the lookup table will have little to no [measurable] effect.
The access pattern to x is independent. You'll get best performance if you access x in a linear manner (e.g. for (i = 0; i < 999999999; ++i) x[i]). If you access it in a semi-random fashion, it will put a strain on the H/W cache logic and its ability to keep the needed/wanted x values "cache hot"
Even with linear access, because x is so large, by the time you get to the end, the first elements will have been evicted from the H/W cache (e.g. most CPU caches are on the order of a few megabytes)
However, if x only has values in a limited range, changing the type from int x[...] to short x[...] or even char x[...] cuts the size by a factor of 2x [or 4x]. And, that can have a measurable improvement on the performance.
Update: I've added an fripper function to show the fastest way [that I know of] to access the table and x arrays in a loop. I've also added a typedef named xval_t to allow the x array to consume less space (i.e. will have better H/W cache performance).
UPDATE #2:
Per your comments ...
fcached was coded [mostly] to illustrate simple/single access. But, it was not used in the final example.
The exact requirements for inline has varied over the years (e.g. was extern inline). Best use now: static inline. However, if using c++, it may be, yet again different. There are entire pages devoted to this. The reason is because of compilation in different .c files, what happens when optimization is on or off. Also, consider using a gcc extension. So, to force inline all the time:
__attribute__((__always_inline__)) static inline
fripper is the fastest because it avoids refetching globals table_of_values and table_bias on each loop iteration. In fripper, compiler optimizer will ensure they remain in registers. See my answer: Is accessing statically or dynamically allocated memory faster? as to why.
However, I coded an fripper variant that uses fcached and the disassembled code was the same [and optimal]. So, we can disregard that ... Or, can we? Sometimes, disassembling the code is a good cross check and the only way to know for sure. Just an extra item when creating fully optimized C code. There are many options one can give to the compiler regarding code generation, so sometimes it's just trial and error.
Because benchmarking is important, I threw in my routines for timestamping (FYI, [AFAIK] the underlying clock_gettime call is the basis for python's time.clock()).
So, here's the updated version:
#include <malloc.h>
#include <time.h>
typedef long long s64;
#define SUPER_INLINE \
__attribute__((__always_inline__)) static inline
#define VARIABLE_USED(_sym) \
do { \
if (1) \
break; \
if (!! _sym) \
break; \
} while (0)
#define TVSEC 1000000000LL // nanoseconds in a second
#define TVSECF 1e9 // nanoseconds in a second
// tvget -- get high resolution time of day
// RETURNS: absolute nanoseconds
s64
tvget(void)
{
struct timespec ts;
s64 nsec;
clock_gettime(CLOCK_REALTIME,&ts);
nsec = ts.tv_sec;
nsec *= TVSEC;
nsec += ts.tv_nsec;
return nsec;
)
// tvgetf -- get high resolution time of day
// RETURNS: fractional seconds
double
tvgetf(void)
{
struct timespec ts;
double sec;
clock_gettime(CLOCK_REALTIME,&ts);
sec = ts.tv_nsec;
sec /= TVSECF;
sec += ts.tv_sec;
return sec;
)
double *table_of_values;
int table_bias;
double *dummyptr;
// use the smallest of these that can contain the values the x array may have
#if 0
typedef int xval_t;
#endif
#if 0
typedef short xval_t;
#endif
#if 1
typedef char xval_t;
#endif
#define XLEN (1 << 9)
xval_t *x;
// fslow -- your original function
double
fslow(int i)
{
return 1; // whatever
}
// ftablegen -- generate variable table
void
ftablegen(double (*f)(int),int lo,int hi)
{
int len;
table_bias = -lo;
len = hi - lo;
len += 1;
// NOTE: you can do free(table_of_values) when no longer needed
table_of_values = malloc(sizeof(double) * len);
for (int i = lo; i <= hi; ++i)
table_of_values[i + table_bias] = f(i);
}
// fcached -- retrieve cached table data
SUPER_INLINE double
fcached(int i)
{
return table_of_values[i + table_bias];
}
// fripper_fcached -- access x and table arrays
void
fripper_fcached(xval_t *x)
{
double val;
double *dptr;
dptr = dummyptr;
for (int i = 0; i < XLEN; ++i) {
val = fcached(x[i]);
// do stuff with val
dptr[i] = val;
}
}
// fripper -- access x and table arrays
void
fripper(xval_t *x)
{
double *tptr;
int bias;
double val;
double *dptr;
// ensure these go into registers to prevent needless extra memory fetches
tptr = table_of_values;
bias = table_bias;
dptr = dummyptr;
for (int i = 0; i < XLEN; ++i) {
val = tptr[x[i] + bias];
// do stuff with val
dptr[i] = val;
}
}
int
main(void)
{
ftablegen(fslow,-10,10);
x = malloc(sizeof(xval_t) * XLEN);
dummyptr = malloc(sizeof(double) * XLEN);
fripper(x);
fripper_fcached(x);
return 0;
}
You can have negative indices in your arrays. (I am not sure if this is in the specifications.) If you have the following code:
int arr[] = {1, 2 ,3, 4, 5};
int* lookupTable = arr + 3;
printf("%i", lookupTable[-2]);
it will print out 2.
This works because arrays in c are defined as pointers. And if the pointer does not point to the begin of the array, you can access the item before the pointer.
Keep in mind though that if you have to malloc() the memory for arr you probably cannot use free(lookupTable) to free it.
I really think Craig Estey is on the right track for building your table in an automatic way. I just want to add a note for looking up the table.
If you know that you will run the code on a Haswell machine (with AVX2) you should make sure your code utilise VGATHERDPD which you can utilize with the _mm256_i32gather_pd intrinsic. If you do that, your table lookups will fly! (You can even detect avx2 on the fly with cpuid(), but that's another story)
EDIT:
Let me elaborate with some code:
#include <stdint.h>
#include <stdio.h>
#include <immintrin.h>
/* I'm not sure if you need the alignment */
double table[8] __attribute__((aligned(16)))= { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 };
int main()
{
int32_t i[4] = { 0,2,4,6 };
__m128i index = _mm_load_si128( (__m128i*) i );
__m256d result = _mm256_i32gather_pd( table, index, 8 );
double* f = (double*)&result;
printf("%f %f %f %f\n", f[0], f[1], f[2], f[3]);
return 0;
}
Compile and run:
$ gcc --std=gnu99 -mavx2 gathertest.c -o gathertest && ./gathertest
0.100000 0.300000 0.500000 0.700000
This is fast!

Calculating the Power spectral density

I am trying to get the PSD of a real data set by making use of fftw3 library
To test I wrote a small program as shown below ,that generates the a signal which follows sinusoidal function
#include <stdio.h>
#include <math.h>
#define PI 3.14
int main (){
double value= 0.0;
float frequency = 5;
int i = 0 ;
double time = 0.0;
FILE* outputFile = NULL;
outputFile = fopen("sinvalues","wb+");
if(outputFile==NULL){
printf(" couldn't open the file \n");
return -1;
}
for (i = 0; i<=5000;i++){
value = sin(2*PI*frequency*zeit);
fwrite(&value,sizeof(double),1,outputFile);
zeit += (1.0/frequency);
}
fclose(outputFile);
return 0;
}
Now I'm reading the output file of above program and trying to calculate its PSD like as shown below
#include <stdio.h>
#include <fftw3.h>
#include <complex.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14
int main (){
FILE* inp = NULL;
FILE* oup = NULL;
double* value;// = 0.0;
double* result;
double spectr = 0.0 ;
int windowsSize =512;
double power_spectrum = 0.0;
fftw_plan plan;
int index=0,i ,k;
double multiplier =0.0;
inp = fopen("1","rb");
oup = fopen("psd","wb+");
value=(double*)malloc(sizeof(double)*windowsSize);
result = (double*)malloc(sizeof(double)*(windowsSize)); // what is the length that I have to choose here ?
plan =fftw_plan_r2r_1d(windowsSize,value,result,FFTW_R2HC,FFTW_ESTIMATE);
while(!feof(inp)){
index =fread(value,sizeof(double),windowsSize,inp);
// zero padding
if( index != windowsSize){
for(i=index;i<windowsSize;i++){
value[i] = 0.0;
}
}
// windowing Hann
for (i=0; i<windowsSize; i++){
multiplier = 0.5*(1-cos(2*PI*i/(windowsSize-1)));
value[i] *= multiplier;
}
fftw_execute(plan);
for(i = 0;i<(windowsSize/2 +1) ;i++){ //why only tell the half size of the window
power_spectrum = result[i]*result[i] +result[windowsSize/2 +1 -i]*result[windowsSize/2 +1 -i];
printf("%lf \t\t\t %d \n",power_spectrum,i);
fprintf(oup," %lf \n ",power_spectrum);
}
}
fclose(oup);
fclose(inp);
return 0;
}
Iam not sure about the correctness of the way I am doing this, but below are the results i have obtained:
Can any one help me in tracing the errors of the above approach
Thanks in advance
*UPDATE
after hartmut answer I'vve edited the code but still got the same result :
and the input data look like :
UPDATE
after increasing the sample frequencyand a windows size of 2048 here is what I've got :
UPDATE
after using the ADD-ON here how the result looks like using the window :
You combine the wrong output values to power spectrum lines. There are windowsSize / 2 + 1 real values at the beginning of result and windowsSize / 2 - 1 imaginary values at the end in reverse order. This is because the imaginary components of the first (0Hz) and last (Nyquist frequency) spectral lines are 0.
int spectrum_lines = windowsSize / 2 + 1;
power_spectrum = (double *)malloc( sizeof(double) * spectrum_lines );
power_spectrum[0] = result[0] * result[0];
for ( i = 1 ; i < windowsSize / 2 ; i++ )
power_spectrum[i] = result[i]*result[i] + result[windowsSize-i]*result[windowsSize-i];
power_spectrum[i] = result[i] * result[i];
And there is a minor mistake: You should apply the window function only to the input signal and not to the zero-padding part.
ADD-ON:
Your test program generates 5001 samples of a sinusoid signal and then you read and analyse the first 512 samples of this signal. The result of this is that you analyse only a fraction of a period. Due to the hard cut-off of the signal it contains a wide spectrum of energy with almost unpredictable energy levels, because you not even use PI but only 3.41 which is not precise enough to do any predictable calculation.
You need to guarantee that an integer number of periods is exactly fitting into your analysis window of 512 samples. Therefore, you should change this in your test signal creation program to have exactly numberOfPeriods periods in your test signal (e.g. numberOfPeriods=1 means that one period of the sinoid has a period of exactly 512 samples, 2 => 256, 3 => 512/3, 4 => 128, ...). This way, you are able to generate energy at a specific spectral line. Keep in mind that windowSize must have the same value in both programs because different sizes make this effort useless.
#define PI 3.141592653589793 // This has to be absolutely exact!
int windowSize = 512; // Total number of created samples in the test signal
int numberOfPeriods = 64; // Total number of sinoid periods in the test signal
for ( n = 0 ; n < windowSize ; ++n ) {
value = sin( (2 * PI * numberOfPeriods * n) / windowSize );
fwrite( &value, sizeof(double), 1, outputFile );
}
Some remarks to your expected output function.
Your input is a function with pure real values.
The result of a DFT has complex values.
So you have to declare the variable out not as double but as fftw_complex *out.
In general the number of dft input values is the same as the number of output values.
However, the output spectrum of a dft contains the complex amplitudes for positive
frequencies as well as for negative frequencies.
In the special case for pure real input, the amplitudes of the positive frequencies are
conjugated complex values of the amplitudes of the negative frequencies.
For that, only the frequencies of the positive spectrum are calculated,
which means that the number of the complex output values is the half of
the number of real input values.
If your input is a simple sinewave, the spectrum contains only a single frequency component.
This is true for 10, 100, 1000 or even more input samples.
All other values are zero. So it doesn't make any sense to work with a huge number of input values.
If the input data set contains a single period, the complex output value is
contained in out[1].
If the If the input data set contains M complete periods, in your case 5,
so the result is stored in out[5]
I did some modifications on your code. To make some facts more clear.
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include "fftw3.h"
int performDFT(int nbrOfInputSamples, char *fileName)
{
int nbrOfOutputSamples;
double *in;
fftw_complex *out;
fftw_plan p;
// In the case of pure real input data,
// the output values of the positive frequencies and the negative frequencies
// are conjugated complex values.
// This means, that there no need for calculating both.
// If you have the complex values for the positive frequencies,
// you can calculate the values of the negative frequencies just by
// changing the sign of the value's imaginary part
// So the number of complex output values ( amplitudes of frequency components)
// are the half of the number of the real input values ( amplitutes in time domain):
nbrOfOutputSamples = ceil(nbrOfInputSamples/2.0);
// Create a plan for a 1D DFT with real input and complex output
in = (double*) fftw_malloc(sizeof(double) * nbrOfInputSamples);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * nbrOfOutputSamples);
p = fftw_plan_dft_r2c_1d(nbrOfInputSamples, in, out, FFTW_ESTIMATE);
// Read data from input file to input array
FILE* inputFile = NULL;
inputFile = fopen(fileName,"r");
if(inputFile==NULL){
fprintf(stdout,"couldn't open the file %s\n", fileName);
return -1;
}
double value;
int idx = 0;
while(!feof(inputFile)){
fscanf(inputFile, "%lf", &value);
in[idx++] = value;
}
fclose(inputFile);
// Perform the dft
fftw_execute(p);
// Print output results
char outputFileName[] = "dftvalues.txt";
FILE* outputFile = NULL;
outputFile = fopen(outputFileName,"w+");
if(outputFile==NULL){
fprintf(stdout,"couldn't open the file %s\n", outputFileName);
return -1;
}
double realVal;
double imagVal;
double powVal;
double absVal;
fprintf(stdout, " Frequency Real Imag Abs Power\n");
for (idx=0; idx<nbrOfOutputSamples; idx++) {
realVal = out[idx][0]/nbrOfInputSamples; // Ideed nbrOfInputSamples is correct!
imagVal = out[idx][1]/nbrOfInputSamples; // Ideed nbrOfInputSamples is correct!
powVal = 2*(realVal*realVal + imagVal*imagVal);
absVal = sqrt(powVal/2);
if (idx == 0) {
powVal /=2;
}
fprintf(outputFile, "%10i %10.4lf %10.4lf %10.4lf %10.4lf\n", idx, realVal, imagVal, absVal, powVal);
fprintf(stdout, "%10i %10.4lf %10.4lf %10.4lf %10.4lf\n", idx, realVal, imagVal, absVal, powVal);
// The total signal power of a frequency is the sum of the power of the posive and the negative frequency line.
// Because only the positive spectrum is calculated, the power is multiplied by two.
// However, there is only one single line in the prectrum for DC.
// This means, the DC value must not be doubled.
}
fclose(outputFile);
// Clean up
fftw_destroy_plan(p);
fftw_free(in); fftw_free(out);
return 0;
}
int main(int argc, const char * argv[]) {
// Set basic parameters
float timeIntervall = 1.0; // in seconds
int nbrOfSamples = 50; // number of Samples per time intervall, so the unit is S/s
double timeStep = timeIntervall/nbrOfSamples; // in seconds
float frequency = 5; // frequency in Hz
// The period time of the signal is 1/5Hz = 0.2s
// The number of samples per period is: nbrOfSamples/frequency = (50S/s)/5Hz = 10S
// The number of periods per time intervall is: frequency*timeIntervall = 5Hz*1.0s = (5/s)*1.0s = 5
// Open file for writing signal values
char fileName[] = "sinvalues.txt";
FILE* outputFile = NULL;
outputFile = fopen(fileName,"w+");
if(outputFile==NULL){
fprintf(stdout,"couldn't open the file %s\n", fileName);
return -1;
}
// Calculate signal values and write them to file
double time;
double value;
double dcValue = 0.2;
int idx = 0;
fprintf(stdout, " SampleNbr Signal value\n");
for (time = 0; time<=timeIntervall; time += timeStep){
value = sin(2*M_PI*frequency*time) + dcValue;
fprintf(outputFile, "%lf\n",value);
fprintf(stdout, "%10i %15.5f\n",idx++, value);
}
fclose(outputFile);
performDFT(nbrOfSamples, fileName);
return 0;
}
If the input of a dft is pure real, the output is complex in any case.
So you have to use the plan r2c (RealToComplex).
If the signal is sin(2*pi*f*t), starting at t=0, the spectrum contains a single frequency line
at f, which is pure imaginary.
If the sign has an offset in phase, like sin(2*pi*f*t+phi) the single line's value is complex.
If your sampling frequency is fs, the range of the output spectrum is -fs/2 ... +fs/2.
The real parts of the positive and negative frequencies are the same.
The imaginary parts of the positive and negative frequencies have opposite signs.
This is called conjugated complex.
If you have the complex values of the positive spectrum you can calculate the values of the
negative spectrum by changing the sign of the imaginary parts.
For this reason there is no need to compute both, the positive and the negative sprectrum.
One sideband holds all information.
Therefore the number of output samples in the plan r2c is the half+1 of the number
of input samples.
To get the power of a frequency, you have to consider the positive frequency as well
as the negative frequency. However, the plan r2c delivers only the right positive half
of the spectrum. So you have to double the power of the positive side to get the total power.
By the way, the documentation of the fftw3 package describes the usage of plans quite well.
You should invest the time to go over the manual.
I'm not sure what your question is. Your results seem reasonable, with the information provided.
As you must know, the PSD is the Fourier transform of the autocorrelation function. With sine wave inputs, your AC function will be periodic, therefore the PSD will have tones, like you've plotted.
My 'answer' is really some thought starters on debugging. It would be easier for all involved if we could post equations. You probably know that there's a signal processing section on SE these days.
First, you should give us a plot of your AC function. The inverse FT of the PSD you've shown will be a linear combination of periodic tones.
Second, try removing the window, just make it a box or skip the step if you can.
Third, try replacing the DFT with the FFT (I only skimmed the fftw3 library docs, maybe this is an option).
Lastly, trying inputting white noise. You can use a Bernoulli dist, or just a Gaussian dist. The AC will be a delta function, although the sample AC will not. This should give you a (sample) white PSD distribution.
I hope these suggestions help.

I am running gbdk a gameboy game making program and I am getting parse errors on the following code

#include <gb/gb.h>
#include <stdio.h>
#include "racecars.h"
/*
RACECARS.C
Tile Source File.
Info:
Form : All tiles as one unit.
Format : Gameboy 4 color.
Compression : None.
Counter : None.
Tile size : 8 x 8
Tiles : 0 to 16
Palette colors : None.
SGB Palette : None.
CGB Palette : None.
Convert to metatiles : No.
This file was generated by GBTD v2.2
*/
/* Start of tile array. */
unsigned char Racecars[] =
{
0x7E,0x7E,0x42,0x5E,0xC3,0xD7,0xC3,0xDF,
0x42,0x52,0x7E,0x7E,0xC3,0xC3,0xC3,0xC3,
0x1C,0x1C,0x36,0x3E,0x62,0x7A,0x43,0x4B,
0x41,0x5D,0x7F,0x7F,0x55,0x55,0x77,0x77,
0x38,0x38,0x44,0xC6,0xAA,0xAA,0xAA,0xAA,
0xAA,0xAA,0x82,0x82,0x44,0xC6,0x38,0x38,
0x18,0x18,0x18,0x18,0x24,0x24,0x42,0x42,
0xFF,0xFF,0x00,0xC6,0x00,0xC6,0x00,0x00,
0x3C,0x3C,0x24,0xE7,0x24,0xE7,0x24,0x24,
0x24,0xE7,0x3C,0xFF,0x24,0x24,0x3C,0x3C,
0x18,0x18,0x24,0xE7,0x42,0xC3,0x42,0x42,
0x42,0x42,0x24,0x66,0x18,0x7E,0x00,0x00,
0xF2,0xF2,0x96,0x96,0xF2,0xF2,0x82,0x82,
0x8F,0x8F,0x00,0x00,0x00,0x00,0x00,0x00,
0xEE,0xEE,0x82,0x82,0x8E,0x8E,0x8B,0x8B,
0xEE,0xEE,0x00,0x00,0x00,0x00,0x00,0x00,
0xEE,0xEE,0x82,0x82,0x8E,0x8E,0x82,0x82,
0xEE,0xEE,0x00,0x00,0x00,0x00,0x00,0x00,
0xE2,0xE2,0x86,0x86,0x82,0x82,0x82,0x82,
0xE7,0xE7,0x00,0x00,0x00,0x00,0x00,0x00,
0xEA,0xEA,0x8A,0x8A,0x8E,0x8E,0x82,0x82,
0xE2,0xE2,0x00,0x00,0x00,0x00,0x00,0x00,
0xE7,0xE7,0x84,0x84,0x87,0x87,0x81,0x81,
0xE7,0xE7,0x00,0x00,0x00,0x00,0x00,0x00,
0xF7,0xF7,0x85,0x85,0x85,0x85,0xB5,0xB5,
0x95,0x95,0xF7,0xF7,0x00,0x00,0x00,0x00,
0x82,0x82,0x82,0x82,0x92,0x92,0x92,0x92,
0x92,0x92,0x92,0x92,0xFE,0xFE,0x00,0x00,
0xFF,0xFF,0x10,0x10,0x10,0x10,0x10,0x10,
0x10,0x10,0xFF,0xFF,0x00,0x00,0x00,0x00,
0x82,0x82,0xC2,0xC2,0xA2,0xA2,0x92,0x92,
0x8A,0x8A,0x86,0x86,0x82,0x82,0x00,0x00,
0x20,0x20,0x30,0x30,0x38,0x38,0x3C,0x3C,
0x20,0x20,0x20,0x20,0x20,0x20,0xFF,0xFF
};
/* End of RACECARS.C */
int main(void){ //starts the main function
int x,y,st,key,z; //define the integers x and y wich will be used to define the hero position, key will be used later
int x1,y1,st1;
int x2,y2,st2;
int x3,y3,st3;
x=20; //set x as 20
y=20; //set y as 20
st=0; //sprite type(1=closed 0=open)
z=0; //set z=0, z will be used for testing if its time to change sprite or not
set_sprite_data(0,76,Racecars); //set our sprite data
wait_vbl_done(); //wait for the visual blank to be done
set_sprite_tile(0,0); //set our tile 0 as the tile 0 of our tile data-pacman open
set_sprite_tile(1,1); //set our tile 1 as the 1st tile of our tile data-pacman closed
set_sprite_tile(2,2);
set_sprite_tile(3,3);
set_sprite_tile(4,4);
set_sprite_tile(5,5);
set_sprite_tile(6,6);
set_sprite_tile(7,7);
set_sprite_tile(8,8);
set_sprite_tile(9,9);
set_sprite_tile(10,10);
set_sprite_tile(11,11);
set_sprite_tile(12,12);
set_sprite_tile(13,13);
set_sprite_tile(14,14);
set_sprite_tile(15,15);
set_sprite_tile(16,16);
move_sprite(st,x,y);
#define MapSizeX 256;
#define MapSizeY 256;
UBYTE ScrollXCnt;
UBYTE ScrollYCnt;
BYTE SCXCnt;
BYTE SCYCnt;
UBYTE tempa, tempb;
UWORD Cnt;
Cnt = 0;
wait_vbl_done();
for(tempa = 0; tempa != 32; tempa++) {
set_bkg_tiles(0,tempa,32,tempa+1,TileMap[Cnt]);
Cnt = Cnt + MapSizeX;
}
if(joypad() & J_UP) ScrollUp();
if(joypad() & J_DOWN) ScrollDown();
if(joypad() & J_LEFT) ScrollLeft();
if(joypad() & J_RIGHT) ScrollRight();
wait_vbl_done();
scroll_bkg(x,y);
ScrollX++; or ScrollX--;
ScrollY++; or ScrollY--;
SCXCnt++; or SCXCnt++;
SCYCnt++; or SCYCnt--;
if(SCXCnt == 8) {
SCXCnt = 0;
Cnt = ScrollX / 8;
Cnt = Cnt + 20;
for(tempa = 0; tempa != 224; tempa++) {
set_bkg_tiles(0,tempa,1,tempa+1,TileMap[Cnt]);
Cnt = Cnt + MapSizeX;
}
}
if(SCXCnt == -8) {
SCXCnt = 0;
SCXCnt = 0;
Cnt = ScrollX / 8;
Cnt--;
for(tempa = 0; tempa != 224; tempa++) {
set_bkg_tiles(0,tempa,1,tempa+1,TileMap[Cnt]);
Cnt = Cnt + MapSizeX;
}
}
.
.
.
That is the c code
I ran the prompt and the following errors popped up.
Parse error before ubyte line 108
Parse error before scroll x
Parse error before scroll y
Parse error before scxCnt
Parse error before ScyCnt
Parse error before 32
Your issue is that you are tying to define new variables in the middle of the function. The GBDK compiler does not allow that, for whatever reason. (This is apparently part of the ANSI C / C89 standard)
You can fix this by changing the top of your main() from
int main(void){ //starts the main function
int x,y,st,key,z; //define the integers x and y wich will be used to define the hero position, key will be used later
int x1,y1,st1;
int x2,y2,st2;
int x3,y3,st3;
to this:
int main(void){ //starts the main function
int x,y,st,key,z; //define the integers x and y wich will be used to define the hero position, key will be used later
int x1,y1,st1;
int x2,y2,st2;
int x3,y3,st3;
//These variables are used later on.
UBYTE ScrollXCnt;
UBYTE ScrollYCnt;
BYTE SCXCnt;
BYTE SCYCnt;
UBYTE tempa, tempb;
UWORD Cnt;
Alternatively, you could move all of the code starting at those variables to another function, and then those variables will be declared with the function and all will be fine.
You might be missing an include file which defines UBYTE.
That UBYTE error shouldn't occur with current sdcc 4.1.0 and therefore gbdk-2020 4.0.3
GBDK's last update was 2001 or so...
Though the other errors are coming from your code being invalid.
That's no valid C code:
ScrollX++; or ScrollX--;
ScrollY++; or ScrollY--;
SCXCnt++; or SCXCnt++;
SCYCnt++; or SCYCnt--;
And I don't think this is doing what you want:
#define MapSizeX 256;
#define MapSizeY 256;
Defines are always unscoped since they are handled by the preprocessor and not the compiler. And you rather want #define MapSizeX 256U
What you wrote will work with your code.
Cnt = Cnt + MapSizeX; turning into Cnt = Cnt + 256;; isn't a big deal, but it would likely break later.

Global static variable suddenly becoming 0 in local function - why?

I've got some general header where I declare it (in std.h):
static volatile unsigned int timestamp;
I've got Interruption where I increase it (in main.c):
void ISR_Pit(void) {
unsigned int status;
/// Read the PIT status register
status = PIT_GetStatus() & AT91C_PITC_PITS;
if (status != 0) {
/// 1 = The Periodic Interval timer has reached PIV since the last read of PIT_PIVR.
/// Read the PIVR to acknowledge interrupt and get number of ticks
///Returns the number of occurrences of periodic intervals since the last read of PIT_PIVR.
timestamp += (PIT_GetPIVR() >> 20);
//printf(" --> TIMERING :: %u \n\r", timestamp);
}
}
in another module I've got procedure where I must use it (in meta.c):
void Wait(unsigned long delay) {
volatile unsigned int start = timestamp;
unsigned int elapsed;
do {
elapsed = timestamp;
elapsed -= start;
//printf(" --> TIMERING :: %u \n\r", timestamp);
}
while (elapsed < delay);
}
first printf shows correct increasing timestamp but Wait printf always shows 0. Why?
You declare your variable as static, which means its local to the file it is included in. The timestamp in main.c is different than the one in meta.c.
You can fix that by declaring timestamp in main.c like so:
volatile unsigned int timestamp = 0;
and in meta.c like so:
extern volatile unsigned int timestamp;

Resources