Page MenuHomec4science

Utils_PSD.py
No OneTemporary

File Metadata

Created
Sat, Feb 8, 17:02

Utils_PSD.py

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 29 23:08:34 2021
@author: srpv
contact: vigneashwara.solairajapandiyan@empa.ch
The codes in this following script will be used for the publication of the following work
"Dynamics of in-situ alloying of Ti6Al4V-Fe by means of acoustic emission monitoring
supported by operando synchrotron X-ray diffraction"
@any reuse of this code should be authorized by the first owner, code author
"""
# %%
# Librariies to import
import numpy as np
import pandas as pd
from scipy.signal import periodogram
import os
#import librosa
print(np.__version__)
# %%
# Helper function
''' The following function helps to get the bands/ Windows'''
def get_band(band_size, band_max_size):
band_window = 0
band = []
for y in range(band_size):
band.append(band_window)
band_window += band_max_size / band_size
return band
''' The following function helps to get the energy in the specific bands bands/ Windows'''
def spectrumpower(psd, band, freqs, band_size):
length = len(band)
Feature_deltapower = []
for i in range(band_size-1):
if i <= (len(band)):
ii = i
# print('low frequencies :',band[ii])
low = band[ii]
ii = i+1
high = band[ii]
# print('high frequencies :',band[ii])
idx_delta = np.logical_and(freqs >= low, freqs <= high)
total_power = sum(psd)
delta_power = sum(psd[idx_delta])
delta_rel_power = delta_power / total_power
Feature_deltapower.append(delta_power)
return Feature_deltapower
''' Calls the functions --> get_band, spectrumpower'''
def custom_function(signal_window, sample_rate, band_size, band_max_size):
i = 0
win = 4 * sample_rate
freqs, psd = periodogram(signal_window, sample_rate, window='hamming')
band = get_band(band_size, band_max_size)
print(band)
# PSD absolute and relative power in each band 10 Features
Feature1 = spectrumpower(psd, band, freqs, band_size)
Feature = np.asarray(Feature1)
# Create the size of numpy array, by checking the size of "Feature_1" and creating "Feature_vectors" with the required shape on first run
if i == 0:
# print("--reached")
size_of_Feature_vectors = int(len(Feature))
size_of_dataset = int(len(signal_window))
Feature_vectors = np.empty((0, size_of_Feature_vectors))
rawdataset = np.empty((0, size_of_dataset))
# print(label)
Feature_vectors = np.append(Feature_vectors, [Feature], axis=0)
rawdataset = np.append(rawdataset, [signal_window], axis=0)
return Feature_vectors # computed feaures
# %%
''' Calls the functions --> custom_function, get_band, spectrumpower...
... helps to compute Power spectral density for a given window.'''
def PSD_function(data_new, sample_rate, band_size, band_max_size):
columnsdata = data_new.transpose()
columns = np.atleast_2d(columnsdata).shape[1]
featurelist = []
classlist = []
rawlist = []
band_size = band_size+1 # interval correction
# for row in loop:
for k in range(columns):
val = columnsdata[:, k]
# totaldatapoints= val.size
# window=round(totaldatapoints/windowsize)
Feature_vectors = custom_function(val, sample_rate, band_size, band_max_size)
print(k)
for item in Feature_vectors:
featurelist.append(item)
return featurelist

Event Timeline