sDAP 2022默认使用IronPython执行脚本,客户电脑无需安装Python环境,但是由于IronPython的限制,无法执行调用原生C接口的的Python脚本。
如果要使用numpy、scipy等使用C接口的Python脚本,则需要安装CPython环境。
使用CPython执行脚本步骤:
安装Pyton 2.7 或 3.X。
添加系统环境变量 PYTHONHOME,值为 python.exe 所在的目录。
添加系统环境变量 PYTHONPATH, 值为 site-packages 文件夹所在的目录(通常为 Python安装目录/Lib/site-packages)。
点击帮助——设置,打开系统设置窗口,切换到脚本设置页面。选择使用CPython,输入Python dll的路径点击保存。
示例1:
使用scipy计算信号的PSD:
# ----------------------------------------------------------
# -- Python Script File
# -- Created on Sunday, October 23, 2022 12:38 PM
# ----------------------------------------------------------
import sys
import clr
import System
import numpy as np
from scipy import signal
# -- Use tab indentation by default (8 spaces) --
# -- Beginning of user code --
# -- This is the entry point of the script, do not modify the method name --
def main():
dap.sSwitchActiveDocument(0)
# Any changes to the channel will be saved, if you do not want to affect the original data use sNewChannel() to create a new channel.
channel = dap.ActiveDocument.sGetChannel(0)
arr = np.array(channel.DataY)
freqs, psd = signal.welch(arr, channel.SampleRate,window='hann',nperseg=4096,noverlap=2048)
channel.DataY = np.array(psd)
channel.DataX = np.array(freqs)
channel.UnitX = 'Hz'
channel.NameX = 'Frequency'
dap.ActiveDocument.sShowFrequencyDomain(channel)
运行结果,与sDAP计算结果对比,红色为Python计算结果,绿色为sDAP计算结果,仅在0Hz存在差异,与直流分量的处理方式有关系,约为2倍。:
示例2:
使用scipy计算两个信号的相关性:
# ----------------------------------------------------------
# -- Python Script File
# -- Created on Friday, June 24, 2022 10:12 AM
# ----------------------------------------------------------
import sys
import clr
import System
import numpy as np
from scipy import signal
# https://gist.github.com/CitizenInsane/c8d3ddc5b14faceec433af3e940e22a8
import arrayConverter
# -- Use tab indentation by default (8 spaces) --
# -- Beginning of user code --
# -- This is the entry point of the script, do not modify the method name --
def main():
# Any changes to the channel will be saved, if you do not want to affect the original data use sNewChannel() to create a new channel.
channel1 = dap.ActiveDocument.sGetChannel('Displacement')
channel2 = dap.ActiveDocument.sGetChannel('Velocity')
sig1 = arrayConverter.asNumpyArray(channel1.DataY)
sig2 = arrayConverter.asNumpyArray(channel2.DataY)
corr = signal.correlate(sig1, sig2)
lags = signal.correlation_lags(len(sig1), len(sig2))
corr /= np.max(corr)
l = len(corr)
arrX = System.Array[System.Double](l)
arrY = System.Array[System.Double](l)
for i in range(l):
arrY[i] = corr[i].real.item()
arrX[i] = lags[i].real.item()
channel = dap.ActiveDocument.sNewChannel()
channel.DataY = arrY
channel.DataX = arrX
channel.NameY = 'correlation'
channel.NameX = 'Lag'
channel.ChannelName = 'Cross-correlated signal'
dap.ActiveDocument.sShowFrequencyDomain(channel)
信号1:
信号2:
运行结果: