Skip to content

Commit

Permalink
Merge pull request #54 from Kalleculated/documentation
Browse files Browse the repository at this point in the history
Updated DocStrings
  • Loading branch information
Streetboy-Ramz authored Jul 15, 2024
2 parents 2cde211 + c2a2013 commit 7041760
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 24 deletions.
54 changes: 47 additions & 7 deletions src/fft_analysator/analysis/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,38 @@ class Preprocess:
internal Signalprocessing class. It contains information about the block size, data, channel count and size and
sample frequency. It is able to return the complete data as a Numpy array or iterate over the defined block size.
Args:
file_paths (object): Get the callback to the data object
block_size (int): Length of data block.
Attributes:
file_paths (string or path): A String or Path to import.
block_size (int): Block size for processing.
table_key (string): Table key names.
source (Acoular.TimeSamples): Acoular TimeSamples class.
source_result (generator): Generator of the current block result.
selected_data_block (Numpy Array): Selected data block.
current_block_idx (int): Index of the current data block.
selected_channel_data (Numpy Array): All data of selected channel.
Methods:
reinitialize_source(): Reinitializes the generator
set_channel_data(int): Gives the complete data of a channel
set_channel_on_data_block(int): Gives the data block of a channel
set_next_data_block(): Gives the next data block
set_current_channel(int): Changes the current selected channel
set_data_block_to_idx(int): Returns the data block of an idx
get_channel_size(): Gets the channel size
get_channel_count(): Gets the channel count
get_abtastrate(): Gets the sample frequency
get_table_names(): Gets the table names
"""

def __init__(self, file_paths=None, block_size=1024):
"""
Constructs all the necessary attributes for the Preprocess object.
Args:
file_paths (object): Get the callback to the data object
block_size (int): Length of data block.
"""

self.file_paths = file_paths
self.block_size = block_size
Expand All @@ -26,9 +52,7 @@ def __init__(self, file_paths=None, block_size=1024):
self.selected_data_block = next(self.source_result)

self.current_block_idx = 0
self.data = None
self.channel_count = None
self.channel_size = None

self.selected_channel_data = np.array([])

def reinitialize_source(self):
Expand Down Expand Up @@ -61,6 +85,8 @@ def set_channel_on_data_block(self, channel):
Args:
channel (int): Channel number.
Returns:
Numpy Array
"""
return self.selected_data_block[:, channel]

Expand Down Expand Up @@ -103,6 +129,9 @@ def set_data_block_to_idx(self, idx):
def get_channel_size(self):
"""
Get_channel_size returns the size of the current channel
Returns:
size (int): Channel size
"""
#size = np.array(self.source)[:, self.current_channel].shape[0]
size = np.array(self.converted_file)[:, self.current_channel].shape[0]
Expand All @@ -111,23 +140,34 @@ def get_channel_size(self):
def get_channel_count(self):
"""
Get_channel_count returns the number of channels of the current .h5 file
Returns:
count (int): Number of channels
"""
count = self.source.numchannels

return count

def get_abtastrate(self):
"""
Get_abtastrate returns the sample_freq from the current .h5 file via Acoular
Returns:
abtasterate (int): Sample Frequency
"""
abtastrate = self.source.sample_freq
return abtastrate

def get_table_names(self):
"""
Get_table_names returns a list of all data set keyword contained in the current .h5 File
Returns:
keys (list): A list of table names
"""
with h5py.File(self.file_paths, 'r') as file:
# Zugriff auf den gewünschten Datensatz
keys = list(file.keys())


return keys
97 changes: 80 additions & 17 deletions src/fft_analysator/analysis/signal_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,54 @@ class Signal_Process:
two signals, frequency and impulse response between input and output data and cross/auto correlation between two
given signals.
Args:
file_path (object): Get the callback to the data object
window (string): window function for the fourier transformation: Allowed options: 'Rectangular','Hanning',
'Hamming', 'Bartlett', 'Blackman'
block_size (int): Length of data block. Allowed values: 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536
overlap (string): Overlap percentage between two blocks for the Welch-Method. Allowed options: 'None','50%',
'75%','87.5%'
Attributes:
file_path (string or path): A String or Path to import.
window (string): Window name for the FFT
block_size (int): Block size for processing.
overlap (string): Overlap percentage
channels (list): List of channels
current_data (Numpy Array): Current Data for the Signal tab
impulse_response_data (Numpy Array): Data for the Impulse response
amplitude_response_data (Numpy Array): Data for the Amplitude response
phase_response_data (Numpy Array): Data for the Phase
data_callback (object):
p0 (int): Is equal to 20*10**-6. Auditory threshold
source (Acoular MaskedTimeSamples): MaskedTimeSamples class to filter out channles
abtastrate (int): Sample frequency
numchannels_total (int): Total numbers of channels
invalid_channel_list (list): Invalid channel list
powerspectra (Acoular Powerspectra): Acoular Powerspectra class for calculation
input_channel (int): Input channel
output_channel (int): Output channel
Methods:
set_parameters(int, str, str): Sets Parameters
invalid_channels(list): Filters all invalid channels
create_time_axis(int): Creates time axis
create_frequency_axis(): Creates frequncy axis
create_correlation_axis(int): Create correlation axis
SPL(channel): Calculates Sound Pressure Level
csm(csm_dB=False): Calculates Cross spectral Matrix
coherence(): Calculates Coherence
frequency_response( frq_rsp_dB=True): Calculates frequency response
phase_response(deg=True): Calculates phase response
impuls_response(imp_dB=False): Calculates impulse response
correlation(type=None): Calculates correlation
"""

def __init__(self, channels=[], file_path=None, window='Hanning', block_size=1024, overlap='50%', data_callback=None):
"""
Constructs all the necessary attributes for the Signal_Process object.
Args:
file_path (object): Get the callback to the data object
window (string): window function for the fourier transformation: Allowed options: 'Rectangular', 'Hanning', 'Hamming', 'Bartlett', 'Blackman'
block_size (int): Length of data block. Allowed values: 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536
overlap (string): Overlap percentage between two blocks for the Welch-Method. Allowed options: 'None', '50%', '75%', '87.5%'
"""
self.file_path = file_path
self.window = window
self.block_size = block_size
Expand Down Expand Up @@ -70,6 +108,7 @@ def set_parameters(self, channels, window, overlap):
def invalid_channels(self, valid_channels):
"""
The invalid_channels function fills the invalid_channel_list with two valid channels chosen by the user.
Args:
valid_channels (list): Contains the two selected channels chosen by the user.
"""
Expand All @@ -78,20 +117,28 @@ def invalid_channels(self, valid_channels):
self.source.invalid_channels = self.invalid_channel_list

# create a time axis
def create_time_axis(self,N):
def create_time_axis(self, N):
"""
The create_time_axis function creates a time axis for the x-Axis
Args:
N (int): Size of the axis
Returns:
time_axis (np.array): The time axis.
"""

time_axis = np.arange(N) / self.abtastrate

return time_axis

# create frequency axis
def create_frequency_axis(self):
"""
The create_frequency_axis function creates the x-Axis for the frequency function
Returns:
powerspectra.fftfreq (np.array): The frequency axis
"""

return self.powerspectra.fftfreq()
Expand All @@ -100,8 +147,12 @@ def create_frequency_axis(self):
def create_correlation_axis(self, N):
"""
The create_correlation_axis function creates the x-Axis for the correlation function
Args:
N (int): Size of the axis
Returns:
tau (np.array): The correlation axis
"""

block_size_factor = self.data_callback.source.numsamples / self.block_size
Expand All @@ -112,13 +163,15 @@ def create_correlation_axis(self, N):

return tau

def SPL(self,channel):
def SPL(self, channel):
"""
The SPL function calculates the Sound Pressure Level of the current signal.
Args:
channel (int): Channel number.
Returns:
SPL (int): Sound Pressure Level of a channel
"""

return 20*np.log10(np.abs(self.data_callback.set_channel_on_data_block(channel))/self.p0)
Expand All @@ -129,17 +182,14 @@ def SPL(self,channel):
def csm(self,csm_dB=False):
"""
The csm function calculates the csm (Cross Spectral Matrix) of Acoular for the valid signals. It returns a
three-dimensional array with size (number of frequencies,2,2) where [:, signal1, signal2] is the
three-dimensional array with size (number of frequencies, 2, 2) where [:, signal1, signal2] is the
Cross spectral density between signal1 and signal2.
Args:
window (string): window function for the fourier transformation: Allowed options: 'Rectangular','Hanning',
'Hamming', 'Bartlett', 'Blackman'
block_size (int): Length of data block. Allowed values: 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536
overlap (string): Overlap percentage between two blocks for the Welch-Method. Allowed options: 'None','50%',
'75%','87.5%'
csm_db (boolean): Return the array in dB values.
csm_dB (Boolean): Return the array in dB values.
Returns:
current_data (np.array): The Cross Spectral Matrix
"""

if csm_dB:
Expand All @@ -156,6 +206,9 @@ def coherence(self):
Args:
None
Returns:
current_data (np.array): The coherence
"""
csm_matrix = self.csm()

Expand All @@ -176,7 +229,10 @@ def frequency_response(self, frq_rsp_dB=True):
and Gxx is the Power Spectral Density of signal x.
Args:
db (boolean): Return the array ian dB values.
frq_rsp_dB (boolean): Return the array in dB values.
Returns:
amplitude_response_data (np.array): The frequency response
"""
csm_matrix = self.csm()

Expand All @@ -203,6 +259,9 @@ def phase_response(self, deg=True):
Args:
deg (boolean): Return the array in degrees
Returns:
phase_response_data (np.array): The phase response
"""
csm_matrix = self.csm()

Expand All @@ -225,6 +284,8 @@ def impuls_response(self,imp_dB=False):
Args:
imp_dB (boolean): Return the array in dB values.
Returns:
impulse_response_data (np.array): The impulse response
"""
csm_matrix = self.csm()
if self.input_channel == self.output_channel:
Expand Down Expand Up @@ -258,6 +319,8 @@ def correlation(self,type=None):
Args:
type (string): Determines if an auto or cross correlation is being calculated. Allowed options: 'xx', 'yy', 'xy'
Returns:
current_data (np.array): The correlation data
"""
csm_matrix = self.csm()
N = len(csm_matrix[:, 0, 0])
Expand Down

0 comments on commit 7041760

Please sign in to comment.