传感器数据的时频分析(Python)

B站影视 2025-01-22 07:23 2

摘要:import osprint(os.path.exists('/content/B500.mat'))Trueimport scipy.io# Load the .mat filemat = scipy.io.loadmat('/content/B500.ma

import osprint(os.path.exists('/content/B500.mat'))Trueimport scipy.io# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat',appendmat=False)# Print the keys (variable names) in the loaded .mat fileprint(mat.keys)dict_keys(['__header__', '__version__', '__globals__', 'data', 'fs', 'rpm', 'ru'])import scipy.ioimport matplotlib.pyplot as plt# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat',appendmat=False)# Check the keys in the loaded .mat fileprint(mat.keys)# If there is an array representing an imageif 'ru' in mat:# Access the image dataimage_data = mat['ru']# Display the image (optional)plt.imshow(image_data)plt.axis('off')plt.show# Save the image as PNGplt.imsave('output_image.png', image_data)else:print('No image data found in the .mat file.')dict_keys(['__header__', '__version__', '__globals__', 'data', 'fs', 'rpm', 'ru'])import scipy.ioimport numpy as npimport matplotlib.pyplot as plt# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat',appendmat=False)# Extract individual components from the dictionarydata = mat['data']fs = mat['fs']rpm = mat['rpm']ru = mat['ru']# Determine dimensions of data arraysnum_samples, num_channels = data.shape# Plot sensor data for each channelplt.figure(figsize=(12, 6))for i in range(num_channels):plt.subplot(num_channels, 1, i + 1)plt.plot(data[:, i])plt.title(f'Sensor {i+1} Data')plt.xlabel('Time')plt.ylabel('Sensor Reading')# Combine all plots into a single imageplt.tight_layoutplt.savefig('combined_sensor_data.png')# Display the combined image (optional)plt.showimport numpy as npimport matplotlib.pyplot as pltfrom scipy.io import wavfile# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat', appendmat=False)# Extract individual components from the dictionarydata = mat['data']# Combine sensor data into a single waveformwaveform = np.mean(data, axis=1) # You can adjust how you combine the data (e.g., using mean, sum, etc.)# Normalize the waveform to the range [-1, 1] (optional)waveform /= np.max(np.abs(waveform))# Save the waveform as a .wav filewavfile.write('combined_sensor_data.wav', int(fs), waveform)print("Waveform saved as combined_sensor_data.wav")import numpy as npimport matplotlib.pyplot as pltfrom scipy.io import wavfile# Load the .wav filesample_rate, waveform = wavfile.read('combined_sensor_data.wav')# Calculate time arrayduration = len(waveform) / sample_ratetime = np.linspace(0., duration, len(waveform))# Plot the waveformplt.figure(figsize=(10, 4))plt.plot(time, waveform)plt.xlabel('Time (s)')plt.ylabel('Amplitude')plt.title('Waveform')plt.grid(True)plt.showimport numpy as npimport matplotlib.pyplot as pltfrom scipy.io import wavfilefrom scipy.signal import spectrogram, kaiser# Load the .wav filefs, x = wavfile.read('combined_sensor_data.wav')# Define time arrayt = np.arange(0, len(x)/fs, 1/fs)# Compute STFTf, t, s = spectrogram(x, fs, window=kaiser(256, 5), noverlap=220, nfft=512)# Compute magnitude squared of STFTsdb = 10 * np.log10(np.abs(s))# Plot spectrogramplt.figure(figsize=(10, 6))plt.pcolormesh(t, f/1000, sdb, shading='gouraud', cmap='viridis')plt.xlabel('Time (s)')plt.ylabel('Frequency (kHz)')plt.title('Spectrogram')plt.showimport numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import make_axes_locatable# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat')# Extract individual components from the dictionarydata = mat['data']fs = mat['fs']rpm = mat['rpm']ru = mat['ru']# Determine dimensions of data arraysnum_samples, num_channels = data.shapest=int(num_samples/100)end = st + st# Create a figurefig, ax1 = plt.subplots(figsize=(12, 8))data_for_specgram = data[st:end, 0]# Plot spectrogram of sensor dataspec, freqs, t, im = ax1.specgram(data_for_specgram, Fs=float(fs), NFFT=256, cmap='twilight')ax1.set_title('Spectrogram of Sensor Data')ax1.set_xlabel('Time')ax1.set_ylabel('Frequency')# Adjust layout# Save the imageplt.savefig('combined_sensor_data.png')# Display the image (optional)plt.showimport scipy.ioimport numpy as np# Load .mat filemat_data = scipy.io.loadmat('/content/B500.mat')# Display all numeric data for every keyfor key, value in mat_data.items:print(f"Key: {key}")if isinstance(value, (int, float, complex, list, np.ndarray)):print(value)elif isinstance(value, dict): # Handle nested dictionariesfor nested_key, nested_value in value.items:if isinstance(nested_value, (int, float, complex, list, np.ndarray)):print(f" {nested_key}: {nested_value}")Key: __header__Key: __version__Key: __globals__Key: data[[-0.34357557][-0.07140313][ 0.18997874]...[-1.82823065][-0.59752596][ 0.74405342]]Key: fs[[24.93]]Key: rpm[[-1.23141266e-04][-2.18301926e-04][-1.76079497e-04]...[ 1.45409845e+03][ 1.45409941e+03][ 1.45410047e+03]]Key: ru[[-0.00314207][-0.00244709][-0.00260559]...[-0.65266762][ 0.25275306][-0.22923205]]import numpy as npimport matplotlib.pyplot as pltfrom scipy.signal import stftimport scipy.iofrom mpl_toolkits.axes_grid1 import make_axes_locatable# Load the .mat filemat = scipy.io.loadmat('/content/B500.mat')# Extract individual components from the dictionarydata = mat['data']fs = mat['fs']rpm = mat['rpm']ru = mat['ru']# Determine dimensions of data arraysnum_samples, num_channels = data.shape# Create a figurefig, ax1 = plt.subplots(figsize=(12, 8))data_for_stft = data[:, 0]# Compute STFTf, t, Zxx = stft(data_for_stft, fs=float(fs), nperseg=256)# Plot STFTim = ax1.pcolormesh(t, f, np.abs(Zxx), shading='gouraud', cmap='viridis')ax1.set_title('STFT of Sensor Data')ax1.set_xlabel('Time [s]')ax1.set_ylabel('Frequency [Hz]')# Add colorbardivider = make_axes_locatable(ax1)cax = divider.append_axes("right", size="5%", pad=0.05)plt.colorbar(im, cax=cax)# Save the imageplt.savefig('stft_sensor_data_time_frequency.png')# Display the imageplt.show知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1担任《Mechanical System and Signal Processing》《中国电机工程学报》等期刊审稿专家,擅长领域:信号滤波/降噪,机器学习/深度学习,时间序列预分析/预测,设备故障诊断/缺陷检测/异常检测。分割线分割线分割线分割线分割线分割线分割线分割线分割线分割线

非平稳信号的一种维格纳-维尔分布(WV分布)中交叉项的消除方法(基于滑动模式奇异谱分析)(MATLAB)

完整代码可通过知乎付费咨询获得:https://www.zhihu.com/consult/people/792359672131756032一维时间序列信号的稀疏度度量方法(MATLAB R2018A)算法运行环境为MATLAB R2018A,执行一维信号的稀疏度量方法,包括峰度(Kurt)、负熵(NE)、d -范数(DN)、2-范数与1-范数之比(L2/L1)、基尼指数(GI)、修正平滑指数(MSI)、基尼指数2 (GI2)、基尼指数3 (GI3)、广义基尼指数(GGI)、完全广义基尼指数等。算法可迁移至金融时间序列,地震信号,机械振动信号,语音信号,声信号,生理信号(EEG,EMG)等一维时间序列信号。完整代码可通过知乎付费咨询获得:https://www.zhihu.com/consult/people/792359672131756032

基于稀疏学习的转子断条故障诊断(MATLAB)

基于群稀疏学习的旋转机械故障诊断方法(MATLAB)

基于改进贝叶斯学习的旋转机械故障诊断(MATLAB)

来源:潜水侠科技

相关推荐