This repository was archived by the owner on Feb 24, 2021. It is now read-only.
forked from justinfchin/helios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
96 lines (83 loc) · 2.25 KB
/
process.py
File metadata and controls
96 lines (83 loc) · 2.25 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
89
90
91
92
93
94
95
96
'''
Title: Functions to run other
Author 1: Justin Chin
Author 2: Sunny Mei
How to use: from process import `function`
Or: import process as `p`
'''
'''########## PACKAGES ##########'''
# Import necessary packages
import scipy.io as sio # for importing matlab file
import matplotlib.pyplot as plt # for plotting
import scipy.io.wavfile as wav # for saving as .wav file
import numpy as np
import os # for opening file
import math # for log calculations
import matlab # for matlab functions
'''########## FILE MANIPULATION ##########'''
# Loads matlab data into a variable
def load(matlabfile):
"""
Parameters
----------
matlabfile : str
the matlab filename
Returns
-------
data : numpy array
the matlab data
"""
matdata = sio.loadmat(matlabfile) # a dictionary
data = matdata['data'] # get value associated with keyword in dictionary
return data
#normal plotting function
#takes in data we want to plot, name of the file
#saves it as a png file
def plot(data, filename):
plt.figure(figsize=(20,10))##change the size of the charts
plt.plot(data)
plt.show()
#iplt.savefig(filename+".png",bbox_inches='tight')
# Saves data as a .wav file
def save_wav(data, filename):
"""
Parameters
----------
data : array
the data to be saved as .wav
filename : str
file name of the new .wav
"""
wav.write(filename+".wav", 44100, ((data + data.min()) * (2 ** 15) / data.ptp()).astype(np.int16))
# Opens the file indicated by filename
def play(filename):
"""
Parameters
----------
filename : str
the file to be opened
"""
os.system("open "+filename)
'''########## SIGNAL PROCESSING ##########'''
# SNR
# Note: that scipy.stats.signaltonoise is defined as mean divided by standard deviation which is usually for images.
# we want S/N = 20 log10(Asignal/Anoise)
### MAKE SURE THIS IS CORRECT
def snr(array):
"""
Parameters
----------
array : array
matlab data
Returns
-------
snr in dB (decibels)
"""
mean = np.mean(array)
sd = np.std(array)
snr = mean/sd
return 10*math.log(snr,10) # returns dB
'''########## TESTING ###########'''
# delete this afterwards
if __name__ == '__main__':
print("HIII")