borisbn

sgn reader

Aug 9th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. from ctypes import *
  2. import mmap
  3.  
  4. class SgnHeader(Structure):
  5.     _pack_ = 1
  6.     _fields_ = [ ("ID", c_char * 64),
  7.                  ("headLen", c_ulong),
  8.                  ("RemarkLen", c_ulong),
  9.                  ("usPackingType", c_ushort),
  10.                  ("Squared", c_ushort),
  11.                  ("SampleSize", c_ushort),
  12.                  ("FreqDiskr", c_double),
  13.                  ("dBand", c_double),
  14.                  ("dCF", c_double),
  15.                  ("tSamplesOrigin", c_ulonglong) ]
  16.  
  17. class Ipp16sc(Structure):
  18.     _pack_ = 1
  19.     _fields_ = [ ("re", c_short), ("im", c_short) ]
  20.  
  21. headerLen = sizeof( SgnHeader )
  22. sampleLen = sizeof( Ipp16sc )
  23.  
  24. def doit():
  25.     global headerLen
  26.     global sampleLen
  27.  
  28.     f = open( "d:/InComing/Analiz/file/0_tone.sgn", "r+b" )
  29.     m = mmap.mmap( f.fileno(), 0 )
  30.     header = SgnHeader.from_buffer_copy( m[:headerLen] )
  31.     samplesCount = ( len( m ) - headerLen ) / sampleLen
  32.     s = Ipp16sc * samplesCount
  33.     samples = s.from_buffer_copy( m[headerLen:] )
  34.     for i in samples[:8192]:
  35.         i.re * i.re + i.im * i.im
  36.     m.close()
  37.     f.close()
  38.  
  39. from timeit import Timer
  40. t = Timer( "doit()", "from __main__ import doit" )
  41. print t.timeit( number = 10 )
Advertisement
Add Comment
Please, Sign In to add comment