以 MATLAB 轉換 OOIBase32 原始檔

本文分享一個 MATLAB 之 m 函數原始檔,可將 OOIBase32 來源之原始文字檔轉換成二個經過三次平滑之一維陣列,並生成一個純文字(.dat)之結果檔案。

OOIBase32 所產生的原始檔,通常長這個樣子。

OOIBase32 Version 2.0.6.5 Data File
++++++++++++++++++++++++++++++++++++
Date: 01-24-2007, 17:39:35
User: Valued Ocean Optics Customer
Spectrometer Serial Number: USB4C02907
Spectrometer Channel: Master
Integration Time (msec): 35
Spectra Averaged: 5
Boxcar Smoothing: 0
Correct for Electrical Dark: Disabled
Time Normalized: Disabled
Dual-beam Reference: Disabled
Reference Channel: Master
Temperature: Not acquired
Spectrometer Type: S4000
ADC Type: USB4000
Number of Pixels in File: 3648
Graph Title: 
>>>>>>Begin Spectral Data<>>>>
178.59 0.000
178.81 0.000
179.02 88.000
179.24 -2350.000
179.46 -14.286
179.67 34.043
179.89 60.000
180.10 -3600.000
180.32 100.000
180.54 1266.667
.
.
.
887.29 110.120
887.46 110.528
887.63 111.647
887.80 109.825
887.97 111.218
888.13 110.559
888.30 109.965
>>>>>>End Spectral Data<>>>>

以下的函式可以將上述原始檔進行轉換(假如已經更改過 OOIBase32 原始檔,那我不保証這個函式能正確運作)。請複製以下原始碼,並儲存為 trans_data.m。

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

function [trans_x,trans_y] = trans_data(source_file,extension,begin,inter,endd)
% Load the source_file.extension,
% then smooth the data and generate a 2*4001 array,
% and creat a source_file.dat.
% out put: trans_x and trans_y: the transformed and smoothe data

%load the source file
source_file_ext = [source_file '.' extension];
fid = fopen(source_file_ext, 'r');

% sweap several beginning lines
for n = 1:30
 s1=fgetl(fid);
end

%scan wavelength
mydata=fscanf(fid,'%g',[2 inf]);
fclose(fid);
xx=mydata(1,:);
yy=mydata(2,:);
trans_x=begin:inter:endd;
trans_y=interp1(xx,yy,trans_x,'cubit');

%write source_file.dat
fid=fopen([source_file '.dat'],'w');
fprintf(fid,'%9.9f %9.9f\n',[trans_x;trans_y]);
fclose(fid);

trans_data 函式共有 5 個引數,分別為來源檔之檔名、來源檔之副檔名、起始值、平滑間距及終止值。舉例來說,我取得一個由 OOIBase32 得到的來源檔,名叫 color.Master.transmission ,其中包括許多 x 與 y 之對應值。我希望將該檔內的資料由 x=300 取至 x=700,且平滑間距為 0.1,轉換成新的 x 叫 new_x 且新的 y 叫 new_y。方法如下:將 trans_data.m 與 color.Master.transmission 置於同一個資料夾,再從 MATLAB 中到達該資料夾,並輸入:

[new_x,new_y]=trans_data('color','Master.transmission',300,0.1,700)

即可,而且也會在該資料夾中生成一個 color.dat 檔。由於其中 color 與 Master.transmission 為字串,故需以單引號括起來。若想觀看結果,可在 MATLAB 中輸入:

plot (new_x,new_y)

以觀看新產生的結果。

以上例為例,trans_data 函式的輸出有二個變數(new_x與new_y)及一個輸出文字檔(color.dat)。你可以根據你的需要,利用這二種不同類型的資料再引入其它函式中操作。

請注意。由於 trans_data 函式會將 OOIBase32 原始檔前 30 列去除(個人的經驗中前 30 列包括不需要的文字說明及無意義資料),若有需要,請修改將函式中 for n = 1:30 的 30 修改成其它數值。