Debian Codegraph @

biosig4c++/python/demo2.py

raw symbols SCIP
1 ####### Demo for Python interface to BioSig" #####################
2 ###
3 ### Copyright (C) 2016-2025 Alois Schloegl <[email protected]>
4 ### This file is part of the "BioSig for Python" repository
5 ### at http://biosig.sf.net/
6 ###
7 ##############################################################
8
9 # download and extract
10 # http://www.biosemi.com/download/BDFtestfiles.zip
11 # into /tmp/
12 # then run this demo
13 #
14 # on linux you can run instead
15 # make test
16
17 import sys
18 import biosig
19 import numpy as np
20 import matplotlib.pyplot as plt
21 import json
22
23 def printf(format, *args):
24 sys.stdout.write(format % args)
25
26
27 ## read header
28 # example from https://github.com/user-attachments/files/19322137/20250314_S5_C1_CellChar.zip
29 FILENAME="20250314_S5_C1_CellChar.cfs"
30 HDR=biosig.header(FILENAME)
31 #print HDR
32 ## extracting header fields
33 H=json.loads(HDR)
34 print(H["Filename"])
35 print(H["TYPE"])
36 print(H["Samplingrate"])
37 Fs=H["Samplingrate"]
38 NS=len(H["CHANNEL"]) # number of channels
39 T0=H["StartOfRecording"]
40 print(Fs,NS,T0)
41
42 ### read and display data ###
43 A=biosig.data(FILENAME)
44 NS=np.size(A,1) # number of channels
45
46 fig = plt.figure()
47 ax = fig.add_subplot(111)
48 h = plt.plot(np.arange(np.size(A,0))/H["Samplingrate"],A[:,0]);
49 ax.set_xlabel('time [s]')
50 # plt.show()
51
52 rec={"dt" : 1000.0/Fs, "xunits": "ms", "channel" : [] }
53 for chan in list(range(NS)):
54 name=H["CHANNEL"][chan]["Label"] # name of channel
55 unit=H["CHANNEL"][chan]["PhysicalUnit"] # units of channel
56 printf("#%d:\t%s\t[%s]\n", chan, name, unit)
57
58 ### get all sweeps, breaks-in-recording
59 selpos=[0]
60 if "EVENT" in H:
61 for k in list(range(len(H["EVENT"]))):
62 if (H["EVENT"][k]["TYP"] == '0x7ffe'):
63 selpos.append(round(H["EVENT"][k]["POS"]*H["Samplingrate"]))
64
65 selpos.append(np.size(A,0))
66
67 ### data from channel m and sweep c can be obtained by:
68 m=0 # channel
69 c=0 # segment, trace, sweep number
70 d = A[selpos[c]:selpos[c+1]-1,m]
71
72 ### display data
73 fig, ax = plt.subplots(nrows=NS)
74 k=0
75 for m in list(range(NS)):
76 for c in list(range(len(selpos)-1)):
77 k = k+1
78 d = np.asarray(A[selpos[c]:selpos[c+1]-1,m])
79 ax[m].plot(np.arange(np.size(d,0))/H["Samplingrate"], d, 'r-')
80 # TODO:
81 # REC[m].append((c,d));
82 # rec["channel"].append((chan, [{"name": name, "yunits": unit, 'section': []}]))
83 # rec["channel"][m]["section"].insert(c,d)
84
85 plt.show()
86