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.

Matlab question. How to export fixed point array to C header?



So, I'm trying to work backwards from the TI supplied FFT filtering example code...what I would like to learn is how to export fixed-point arrays (like the sine waves from the filtering example) to a header that I can include in my CC4 project.

 

The app note mentions that the format of the data is S16Q15. This means that the data is 16-bit word length with 15 digit fractional length, right? I've looked a bit at Matlab's fixed-point toolbox, but I'm unsure how to easily export the data into code composer. Any insight to the process would be greatly appreciated.

 

Jonathan

  • Hi Jonathan,

    Can you please provide some specifics.  Are you refering to the FFT code that we have released in Open Source on code.google, or some other FFT code?  Also what app note are you refering to?

    Best regards, Vishal

  • Vishal,

    Let me clarify. I am referring to the FFT filter code that was recently released on code.google for the C5505 USB Stick. When I mentioned "app note", what I really meant was the release notes that were included with the FFT filter package. Basically, I want to be able to export a sine wave from Matlab in the S16Q15 format. 

     

    I'm sure the steps are quite simple, and I'm hoping that someone here can give me a quick pointer. I have access to Matlab's fixed-point toolbox.

     

    Thanks,

    Jonathan

  • Vishal,

    I believe I answered my own question with a bit more digging. I was able to generate a fixed-point representation in Matlab and export it using fprintf. My issue was that I wasn't aware that I could just use the hex function on the fi object. So for posterity, I'll post this snippet of Matlab code.

    fs=48000;

    t=0:1/fs:0.001;

    x = sin(2*pi*5000*t); % for 5Khz sine

    xfi = fi(x,1,16,15); % S16Q15  signed, 16 bit word, fraction length 15

    fd = fopen('sin5k.txt', 'w');

    for i=1:length(x)

      fprintf( fd,'0x%s, ',hex(xfi(k)) );

    end

    fclose(fd);

    Hopefully others new to the fixed-point toolbox will find this useful.

    Jonathan

  • Here's another way to use MATLAB to write a fixed-point sine wave to file.
    You can copy and paste the contents of this file into Code Composer directly.

     

    fs = 48000;                         % sampling frequency
    N = 48*2;                           % signal length
    sig = zeros(1,N);                   % create sig buffer
    n = 0:(N-1);                       

    %Create 1kHz tone
    fk = 1000;                          % signal frequency
    A = 0.8;                            % peak amplitude
    sig = A * sin(2*pi*n*fk/fs);        % create sampled sine signal

    %Quantize sig to S16Q15 fixed point
    sig_fixed = fi(sig,1,16,15);             % fi(source, signed, word length,  fraction length (best precision)
    sig_fixed_hex = sig_fixed.hex;      % extract hex of sig_fixed

    %plot sig_fixed
    plot(sig_fixed);

    %Split sig_fixed_hex from one string to array of strings
    sig_fixed_hex_split = regexp(sig_fixed_hex,'\s*','split');

    %Print sig_fixed_hex to a file
    file = fopen('fixed-sine.dat','w');
    cols = 8;
    for i = 0:cols:N-1
        for j = 1:cols
            fprintf(file,'0x%s, ',char(sig_fixed_hex_split(i+j)));
        end
        fprintf(file,'\n');
    end

    fclose(file);                             %You must manually delete the comma after the last entry in the file!!

    Hope this helps,
    Mark

  • Thanks Mark,

    This example is cleaner than mine.

    Jonathan