匯出CCS3.3資料及使用matlab處理的方法

來源:互聯網
上載者:User

CCS3.3是一款DSP的整合式開發環境(IDE)。在做DSP開發時,為驗證演算法,常常需要使用matlab進行演算法驗證,驗證演算法就需要資料。因此,一種互動的方法是:

  1. 使用DSP開發板串連CCS
  2. 用CCS3.3菜單中的“File->Data->Save..”功能匯出DSP記憶體中的資料到PC的.dat檔案中
  3. 使用Matlab讀取.dat檔案,進行資料處理

下面給出使用Matlab讀取CCS匯出的dat檔案的方法:

% =========================================================================% Read *dat file of CCS3.3% xiahouzuoxin% 2014.04.21% =========================================================================% clc;clear all;close all;% 對話方塊選擇*.dat檔案[fname,pname]=uigetfile(...    {'*.dat';'*.*'},'Input *.dat File');fid = fopen(fullfile(pname,fname));fseek(fid, 21,-1);  % 去檔案頭, 21位元組,如 1651 1 80000006 0 100% 按指定格式讀取解析fm = 4;switch (fm)    case 4  % 按4Byte格式讀,如 0x 80000000        x = textscan(fid, '%2s %8s');          z(:,1) = hex2dec(x{2});            case 2 % 按2Byte格式讀,如 0x 8000 0000        x = textscan(fid, '%2s %4s %4s');          z(:,1) = hex2dec(x{3});        z(:,2) = hex2dec(x{2});    case 1 % 按1Byte格式讀,如 0x 80 00 00 00        x = textscan(fid, '%2s %2s %2s %2s %2s');          z(:,1) = hex2dec(x{5});        z(:,2) = hex2dec(x{4});        z(:,3) = hex2dec(x{3});        z(:,4) = hex2dec(x{2});    otherwise        z = [];endif ~isempty(z)    % 將資料處理代碼放在這裡endfclose(fid);

程式中預設DSP記憶體中資料的儲存格式為整數類型,而對於浮點型DSP如TMS320C6713B,常常在記憶體中是float或double格式儲存的,

這時,我們還需要一個轉化為浮點數的操作,因此我寫了一個轉化為float類型的函數,

function y = integer2float(x, ishex)% =========================================================================% 有些資料y原本是float類型,在記憶體中是按float格式儲存% 而現在按整數將其從記憶體中讀出變成了x% 這個函數就是用於將按整數格式讀出的x轉變成y% ishex~=0表示輸入為十六進位格式% 註:要完成該函數,你必須瞭解IEEE浮點數的格式%% xiahouzuoxin% 2014.04.21% =========================================================================if nargin == 2    if (ishex)        x = hex2dec(x);    endend[h w] = size(x);y = zeros(h,w);for i = 1:h    for j = 1:w        sign = bitget(x(i,j),32);        exponent = bitget(x(i,j),24:31) * 2.^(0:7).';        fraction = bitget(x(i,j),1:23) * 2.^(-23:-1).';        y(i,j) = (-1)^sign * (1+fraction) * 2^(exponent-127);    endend

根據不同的輸入情況(可以為十六進位的字串表示或十進位數,但必須≤8Bytes),使用方法舉個例子:

>> x='4565A012'>> y=integer2float(x,1);>> x = 23974881923;>> y = integer2float(x)

後來發現,Matlab已經早有對應的方法了,

y = typecast(uint32(z),'single');

可以直接把z轉化為解析為單精確度浮點數,具體使用請參見

>> help typecast

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.