-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_recording_script.py
More file actions
88 lines (72 loc) · 2.68 KB
/
simple_recording_script.py
File metadata and controls
88 lines (72 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""
National Instruments Analog Input Recorder
This program records one analog input channel on National Instruments devices
and saves the data in a binary file along with metadata in CSV format.
Author: Stefan Mucha_admin
Date: Mon Sep 27 16:54:30 2021
"""
import nidaqmx
from nidaqmx import constants
import matplotlib.pyplot as plt
import datetime as dt
import numpy as np
import tkinter as tk
from tkinter import filedialog
import pandas as pd
# Configuration parameters
recording_id = 'test' # Identifier for the recording
input_channels = 1 # Number of input channels
channel_name = 'ai1' # Channel name on the NI device
rec_fs = 100000 # Sampling frequency (Hz)
rec_dur = 10 # Recording duration (seconds)
input_samples = rec_fs * rec_dur # Total number of samples to acquire
# Prompt user to select an output directory
root = tk.Tk()
root.withdraw() # Hide the root window
output_path = filedialog.askdirectory(title="Select folder for storage of recordings")
if not output_path:
raise ValueError("No output directory selected. Exiting...")
# Detect available National Instruments DAQ devices
system = nidaqmx.system.System.local()
if not system.devices:
raise RuntimeError("No NI DAQ device detected. Exiting...")
device_name = system.devices[0].name # Select the first available device
# Setup DAQ read task
read_task = nidaqmx.Task(new_task_name="in")
read_task.ai_channels.add_ai_voltage_chan(
physical_channel=f"{device_name}/{channel_name}",
min_val=-10,
max_val=10,
units=constants.VoltageUnits.VOLTS
)
read_task.timing.cfg_samp_clk_timing(
rate=rec_fs,
sample_mode=constants.AcquisitionType.FINITE,
samps_per_chan=input_samples
)
# Generate timestamp for file naming
string_timestamp = dt.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
# Save metadata as a CSV file
metadata = pd.DataFrame({
'recording_id': [recording_id],
'timestamp': [string_timestamp],
'input_channels': [input_channels],
'sample_rate': [rec_fs],
'duration': [rec_dur]
})
# Start the data acquisition task
data = read_task.read(input_samples, timeout=(rec_dur + 10))
# Stop and close the read task
read_task.stop()
read_task.close()
# Construct file names for data and metadata
data_fname = f"{recording_id}_{string_timestamp}.bin"
meta_fname = f"{recording_id}_{string_timestamp}.csv"
# Save acquired data as a binary file
data_array = np.array(data, dtype=np.float32) # Ensure consistent data type
data_array.tofile(f"{output_path}/{data_fname}")
# Save metadata as a CSV file
metadata.to_csv(f"{output_path}/{meta_fname}", index=False, sep=";")
# Print confirmation message
print(f"Saved file: {data_fname}")