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.

complex addition

Other Parts Discussed in Thread: SYSBIOS

Hi I'm having trouble using complex addition with my C6748 processor. If I try to use complex addition in my main function, it works, but when I call a different function and try to use complex addition within that function, my program just restarts and goes back to the start of my main function. I am using CSS v5.5 and compiler version TI v7.4.4. Is this a memory issue?

Thanks.

  • Hi Jonathan,

    What type of code are you using?

    starterware or DSP app code or SYSBIOS  or CSL code or own code?

    but when I call a different function and try to use complex addition within that function, my program just restarts and goes back to the start of my main function.

    I'm curious about your issue.

    If possible, Could you please share your code to reproduce.

  • Hi Titus,

    I am just writing my own code.

    Here is my code. When it goes through the zeromean_normalization function it normally breaks out and restarts from the beginning. However, if I change my NUM_SAMPLES to 50, it doesn't break. Is this a memory problem?

    #include <stdio.h>
    #include <math.h>
    #include <complex.h>
    
    #define PI 3.14159265358979
    #define NUM_SAMPLES 100
    #define TESTFREQ 800.0
    #define SAMPLING_FREQ 8000.0
    
    
    //zeromean_normalization
    //calculates zero mean normalized signal
    //INPUT: COMPLEX *x - array of signal values
    //		 COMPLEX *mic_signal_no_dc - array to contain output
    void zeromean_normalization(float complex *x, float complex *mic_signal_no_dc)
    {
    	//subtract mean from signal
    
    	int n;
    	float complex mean;
    	float complex numel;
    
    	mean = 0.0 + 0.0*I;
    	numel = NUM_SAMPLES + 0.0*I;
    
    	printf("subtracting mean...\n");
    	for (n = 0; n < NUM_SAMPLES; n++)
    	{
    		mean += x[n];
    	}
    	mean = mean/numel;
    
    	for (n = 0; n < NUM_SAMPLES; n++)
    	{
    		mic_signal_no_dc[n] = x[n] - mean;
    	}
    	printf("mean subtracted!\n");
    	//normalize signal
    	//find norm(euclidean length) of variable
    	printf("normalizing...\n");
    	float complex norm = 0.0 + 0.0*I;
    	for (n = 0; n < NUM_SAMPLES; n++)
    	{
    		norm += pow(crealf(mic_signal_no_dc[n]),2)+0.0*I;
    	}
    	norm = csqrtf(norm);
    
    	for (n = 0; n < NUM_SAMPLES; n++)
    	{
    
    			mic_signal_no_dc[n] = mic_signal_no_dc[n]/norm;
    	}
    	printf("normalized!\n");
    	return;
    
    }
    
    void dft(float complex *x)
    {
    	float complex result[NUM_SAMPLES];
    	int k,n;
    
    	for (k=0; k<NUM_SAMPLES; k++)
    	{
    		result[k] = 0.0 + 0.0*I;
    
    		for (n=0; n<NUM_SAMPLES; n++)
    		{
    			result[k] += crealf(x[n])*cos(2*PI*k*n/NUM_SAMPLES) + cimagf(x[n])*sin(2*PI*k*n/NUM_SAMPLES)
    					+(cimagf(x[n])*cos(2*PI*k*n/NUM_SAMPLES) - crealf(x[n])*sin(2*PI*k*n/NUM_SAMPLES))*I;
    		}
    	}
    	for (k=0;k<NUM_SAMPLES;k++)
    	{
    		x[k] = result[k];
    	}
    }
    void idft(float complex *x)
    {
    	float complex result[NUM_SAMPLES];
    	int k,n;
    
    	for (k=0; k<NUM_SAMPLES; k++)
    	{
    		result[k] = 0.0 + 0.0*I;
    
    		for (n=0; n<NUM_SAMPLES; n++)
    		{
    			result[k] += crealf(x[n])*cos(2*PI*k*n/NUM_SAMPLES) + cimagf(x[n])*sin(2*PI*k*n/NUM_SAMPLES)
    					-(cimagf(x[n])*cos(2*PI*k*n/NUM_SAMPLES) - crealf(x[n])*sin(2*PI*k*n/NUM_SAMPLES))*I;
    		}
    	}
    	for (k=0;k<NUM_SAMPLES;k++)
    	{
    		x[k] = result[k]/NUM_SAMPLES;
    	}
    }
    /*
    *GCC
     * computes generalized cross correlation on 2 signals to find the time delay
     * INPUT: both signals are in the fourier domain already
     * 			mic_signal2 is reference mic
     * OUPUT: returns time delay in samples
     */
    int GCC(float complex *mic_signal1,float complex *mic_signal2)
    {
    	//find complex conjugate of reference mic
    	int i;
    	float complex correlation[NUM_SAMPLES];
    	for(i = 0; i < NUM_SAMPLES; i++)
    	{
    		mic_signal2[i] = conjf(mic_signal2[i]);
    	}
    
    	//calculate GCC
    	for(i=0;i<NUM_SAMPLES;i++)
    	{
    		correlation[i] = mic_signal1[i]*mic_signal2[i];
    	}
    	//switch to PHAT
    	//calculate denominator, max(abs(GCC))
    	float denom = 1*powf(10,-16);
    	for(i=0;i<NUM_SAMPLES;i++)
    	{
    		if(cabsf(correlation[i])>denom)
    		{
    			denom = cabsf(correlation[i]);
    		}
    	}
    	//PHAT calculation
    	for(i = 0; i<NUM_SAMPLES; i++)
    	{
    		correlation[i] = correlation[i]/denom;
    	}
    
    	//ifft, only keep real part
    	float delay[NUM_SAMPLES];
    	idft(correlation);
    	for(i=0; i<NUM_SAMPLES; i++)
    	{
    		delay[i] = crealf(correlation[i]);
    	}
    	return 0;
    }
    
    int main(void) {
    
    	//initialize starting curve
    	int n;
    	int delay;
    	float complex samples[NUM_SAMPLES];
    	float complex samples2[NUM_SAMPLES];
    	int delay1, delay2;
    	float complex signal_norm[NUM_SAMPLES];
    	float complex signal_norm2[NUM_SAMPLES];
    
    	printf("creating samples...\n");
    	delay1 = 75;
    	delay2 = 50;
    	for(n = 0; n <NUM_SAMPLES; n++)
    	{
    		samples[n] = 0.0+0.0*I;
    		samples2[n] = 0.0+0.0*I;
    		if(n == delay1)
    			samples[n] = 1.0+0.0*I;
    		if(n == delay2)
    			samples2[n] = 1.0+0.0*I;
    	}
    
    
    	printf("samples created!\n");
    
    	//data preprocessing
    
    	zeromean_normalization(samples,signal_norm);
    	zeromean_normalization(samples2,signal_norm2);
    	
    	//find fft
    	dft(signal_norm);
    	dft(signal_norm2);
    	printf("fft calculated!\n");
    
    	//calculate GCC
    	printf("calculating GCC");
    
    	delay = GCC(signal_norm,signal_norm2);
    	printf("GCC calculated");
    
    	return 0;
    }
    

    Thanks,

    Jon

  • Hi Titus, were you able to recreate the problem?

  • Hi Jonathan,

    Yes, I am able to reproduce your issue that code will not run properly. It will fall in infinite loop between two "zeromean_normalization" and some times getting "The CIO command cmd" failure, so that I could not run the code properly.

    I have modified the ".cmd" file then I'm not facing any problem.

    The attached linker command file used to run the DSP code in DDR2.

    I can run the code with "NUM_SAMPLES = 100"

    Please try this and let us know the status.

    // ============================================================================
    // Linker Command File for Linking c674 DSP Programs
    //
    // These linker options are for command line linking only. For IDE linking,
    // you should set your linker options in Project Properties.
    //         -c                    Link Using C Conventions
    //        -stack     0x1000        Software Stack Size
    //        -heap    0x1000        Heap Area Size
    // ===========================================================================
    -stack 0x1000
    -heap 0x1000
    
    // ============================================================================
    //                         Specify the System Memory Map
    // ============================================================================
    MEMORY
    {
        L1P:    o = 0x11E00000        l = 0x00008000
        L1D:    o = 0x11F00000        l = 0x00008000
        L2:     o = 0x11800000        l = 0x00040000
        DDR2:   o = 0xC0000000        l = 0x08000000
    }
    
    // ============================================================================
    //                 Specify the Sections Allocation into Memory
    // ============================================================================
    SECTIONS
    {
        .cinit        >        DDR2               // Initialization Tables
        .pinit        >        DDR2               // Constructor Tables
        .init_array   >        DDR2               //
        .binit        >        DDR2               // Boot Tables
        .const        >        DDR2               // Constant Data
        .switch       >        DDR2               // Jump Tables
        .text         >        DDR2               // Executable Code
        .text:_c_int00: align=1024 > DDR2         // Entrypoint
    
        GROUP (NEARDP_DATA)                       // group near data
        {
           .neardata
           .rodata
           .bss                                   // note: removed fill = 0
        }             >        DDR2
        .far: fill = 0x0, load > DDR2             // Far Global & Static Variables
        .fardata      >        DDR2               // Far RW Data
        .stack        >        DDR2               // Software System Stack
        .sysmem       >        DDR2               // Dynamic Memory Allocation Area
    
        .cio          >        DDR2               // C I/O Buffer
        .vecs         >        DDR2               // Interrupt Vectors
    }