zoukankan      html  css  js  c++  java
  • 如何用Matlab处理.wfm格式的数据

      从示波器中捕获的波形处理,此代码源于https://ww2.mathworks.cn/matlabcentral/fileexchange/14918-tektronix-wfm-file-reader

      function:

      1 function [out_descript, outdata, timedata] = wfm_ascii_dpo(fname, data_start, data_stop)
      2 % Converts TSD5/6/7k and DPO7k/70k .wfm file to ASCII format 
      3 % with time array
      4 %
      5 % data_start and data_stop input arguments are optional
      6 % and can be used to read parts of file
      7 %
      8 % To do: implement fast frame, pixel maps
      9 %
     10 out = [];
     11 if nargin==0        
     12     fname='';
     13 end
     14 if isempty(fname)
     15     [filename,pname]=uigetfile({'*.wfm', 'Tektronix Waveform Files (*.wfm)';'*.*', 'All Files (*.*)'},'Choose Tektronix WFM file');
     16     fname=[pname filename];
     17 end
     18 %---Open file
     19 fd = fopen(fname,'r');
     20 if fd==-1
     21     error('Problem opening file "%s"',fname)
     22 end
     23 %---Determine byte ordering, then close and reopen with proper byte ordering
     24 ByteOrder = fread(fd,1,'ushort');
     25 if ByteOrder==61680
     26     fclose(fd);
     27     fd = fopen(fname,'r','ieee-be');
     28 else
     29     fclose(fd);
     30     fd = fopen(fname,'r','ieee-le');
     31 end
     32 %---WFM static file information
     33 out.ByteOrder               = fread(fd, 1,'ushort' );
     34 out.VersionNum              = fread(fd, 8,'*char'  )';
     35 if ~any(strcmp(out.VersionNum,{':WFM#001';':WFM#002';':WFM#003'}))
     36     fclose(fd);
     37     error('File "%s" is not a valid WFM file',fname)
     38 end
     39 out.NumDigitsInByteCount    = fread(fd, 1,'char'   );
     40 out.NumBytesToEOF           = fread(fd, 1,'long'   );
     41 out.NumBytesPerPoint        = fread(fd, 1,'char'   );
     42 out.ByteOffsetToCurveBuffer = fread(fd, 1,'long'   );
     43 out.HorZoomScale            = fread(fd, 1,'long'   );
     44 out.HorZoomPos              = fread(fd, 1,'float32');
     45 out.VerZoomScale            = fread(fd, 1,'double' );
     46 out.VerZoomPos              = fread(fd, 1,'float32');
     47 out.WaveformLabel           = fread(fd,32,'*char'  )';
     48 out.N                       = fread(fd, 1,'ulong'  );
     49 out.HeaderSize              = fread(fd, 1,'ushort' );
     50 %---WFM header
     51 out.SetType                 = fread(fd, 1,'int'   );
     52 out.WfmCnt                  = fread(fd, 1,'ulong' );
     53 jnk                         = fread(fd,36,'uchar' ); % Skip these for now
     54 out.DataType                = fread(fd, 1,'int'   );
     55 jnk                         = fread(fd,28,'uchar' ); % Skip these for now
     56 switch out.VersionNum
     57 case {':WFM#002' ':WFM#003'}
     58     jnk                     = fread(fd, 1,'ushort'); % Skip these for now
     59 end
     60 jnk                         = fread(fd,12,'uchar' ); % Skip these for now
     61 %---Explicit Dimension 1/2
     62 s = [];
     63 for n=1:2
     64     s.DimScale              = fread(fd, 1,'double');
     65     s.DimOffset             = fread(fd, 1,'double');
     66     s.DimSize               = fread(fd, 1,'ulong' );
     67     s.Units                 = fread(fd,20,'*char' );
     68     s.DimExtentMin          = fread(fd, 1,'double');
     69     s.DimExtentMax          = fread(fd, 1,'double');
     70     s.DimResolution         = fread(fd, 1,'double');
     71     s.DimRefPoint           = fread(fd, 1,'double');
     72     s.Format                = fread(fd, 1,'int'   );
     73     s.StorageType           = fread(fd, 1,'int'   );
     74     jnk                     = fread(fd,20,'uchar' ); % Skip these for now
     75     s.UserScale             = fread(fd, 1,'double');
     76     s.UserUnits             = fread(fd,20,'*char' );
     77     s.UserOffset            = fread(fd, 1,'double');
     78     switch out.VersionNum
     79     case ':WFM#003'
     80         s.PointDensity      = fread(fd, 1,'double');
     81     otherwise
     82         s.PointDensity      = fread(fd, 1,'ulong' );
     83     end
     84     s.HRef                  = fread(fd, 1,'double');
     85     s.TrigDelay             = fread(fd, 1,'double');
     86     out.ExplicitDimension(n) = s;
     87 end
     88 %---Implicit Dimension 1/2
     89 s=[];
     90 for n=1:2
     91     s.DimScale              = fread(fd, 1,'double');
     92     s.DimOffset             = fread(fd, 1,'double');
     93     s.DimSize               = fread(fd, 1,'ulong' );
     94     s.Units                 = fread(fd,20,'*char' )';
     95     jnk                     = fread(fd,16,'uchar' ); % Skip these for now
     96     s.DimResolution         = fread(fd, 1,'double');
     97     jnk                     = fread(fd,12,'uchar' ); % Skip these for now
     98     s.UserScale             = fread(fd, 1,'double');
     99     s.UserUnits             = fread(fd,20,'*char' );
    100     s.UserOffset            = fread(fd, 1,'double');
    101     switch out.VersionNum
    102     case ':WFM#003'
    103         s.PointDensity      = fread(fd, 1,'double');
    104     otherwise
    105         s.PointDensity      = fread(fd, 1,'ulong' );
    106     end
    107     s.HRef                  = fread(fd, 1,'double');
    108     s.TrigDelay             = fread(fd, 1,'double');
    109     out.ImplicitDimension(n) = s;
    110 end
    111 %---Time Base 1/2 Information
    112 s=[];
    113 for n=1:2
    114     s.RealPointSpacing      = fread(fd, 1,'ulong' );
    115     s.Sweep                 = fread(fd, 1,'int'   );
    116     s.TypeOfBase            = fread(fd, 1,'int'   );
    117     out.TimeBase(n) = s;
    118 end
    119 %---WFM Update Spec
    120 jnk                         = fread(fd,24,'uchar'); % Skip these for now
    121 %---WFM Curve Information
    122 jnk                         = fread(fd,10,'uchar'); % Skip these for now
    123 PrechargeStartOffset        = fread(fd, 1,'ulong');
    124 DataStartOffset             = fread(fd, 1,'ulong');
    125 PostchargeStartOffset       = fread(fd, 1,'ulong');
    126 PostchargeStopOffset        = fread(fd, 1,'ulong');
    127 EndOfCurveBufferOffset      = fread(fd, 1,'ulong');
    128 %---FastFrame Frames
    129 %OPTIONAL
    130 %---Curve Buffer
    131 out.CurveSizeInBytes = PostchargeStartOffset - DataStartOffset;
    132 out.CurveSize = out.CurveSizeInBytes / out.NumBytesPerPoint;
    133 jnk = fread(fd,DataStartOffset,'uchar'); % Skip precharge
    134 if nargin<3        
    135     data_start = 1;
    136     data_stop = out.CurveSize;
    137 end
    138 switch out.NumBytesPerPoint
    139     case 1
    140         if data_start > 1
    141         jnk = fread(fd,data_start-1,'*int8');
    142         out.CurveData = fread(fd,data_stop-data_start+1,'*int8');
    143         else
    144         out.CurveData = fread(fd,data_stop,'*int8');
    145         end
    146     case 2
    147         if data_start > 1
    148         out.CurveData = fread(fd,data_start-1,'*int16');
    149         out.CurveData = fread(fd,data_stop-data_start+1,'*int16');
    150         else
    151         out.CurveData = fread(fd,data_stop,'*int16');
    152         end
    153 end
    154     
    155 %---Close file
    156 fclose(fd);
    157 %E_DimOffset = out.ExplicitDimension(1,1).DimOffset;
    158 %E_DimScale = out.ExplicitDimension(1,1).DimScale;
    159 %E_CurveData = out.CurveData;
    160 %I_DimOffset = out.ImplicitDimension(1,1).DimOffset
    161 %I_DimScale = out.ImplicitDimension(1,1).DimScale
    162 y = (out.ExplicitDimension(1,1).DimOffset) + (out.ExplicitDimension(1,1).DimScale)*double(out.CurveData); 
    163 t = out.ImplicitDimension(1,1).DimOffset + out.ImplicitDimension(1,1).DimScale*(data_start:data_stop);
    164 out_descript.Fs = 1/out.ImplicitDimension(1,1).DimScale;
    165 out_descript.Ts = out.ImplicitDimension(1,1).DimScale;
    166 out_descript.N = out.CurveSize;
    167 out_descript.byte = out.NumBytesPerPoint;
    168 outdata = y;
    169 timedata = t;
    View Code

      调用函数:

    1 clc
    2 clear all
    3 [out_descript,outdata,timedata]= wfm_ascii_dpo('Tek001.wfm')   % wfm_ascii_dpo(fname,data_start,data_stop)
    4 figure;
    5 plot(timedata * 10^9,outdata * 200,'b-');
    6 xlabel('x')
    7 ylabel('y)')
    8 legend('map')
    View Code
  • 相关阅读:
    js-jQuery对象与dom对象相互转换
    django模板{%for%}中的forloop的应用
    ubuntu MySQL数据库输入中文乱码 解决方案
    多线程下的神奇的IOCP
    类库服务寄宿到WebHost
    细说Asp.Net WebAPI消息处理管道
    项目发布Debug和Release版的区别
    linux yum命令详解
    Linux系统如何查看版本信息
    Linux查看物理CPU个数、核数、逻辑CPU个数
  • 原文地址:https://www.cnblogs.com/571328401-/p/12937868.html
Copyright © 2011-2022 走看看