Skip to content

Instantly share code, notes, and snippets.

View wiccy46's full-sized avatar

Jiajun wiccy46

  • Holoplot
  • Berlin
View GitHub Profile
@wiccy46
wiccy46 / dbfft.py
Created July 25, 2019 21:01
[dbfft] Power Spectrum in dB #python #dsp
def dbfft(x, fs, win=None, ref=32768):
"""
Calculate spectrum in dB scale
Args:
x: input signal
fs: sampling frequency
win: vector containing window samples (same length as x).
If not provided, then rectangular window is used by default.
ref: reference value used for dBFS scale. 32768 for int16 and 1 for float
@wiccy46
wiccy46 / withinErrBar.py
Created July 22, 2019 16:56
[withinErrBar]Within subject error bar #python #DataAnalysis
def within_subject_errorbar(df, ci_r = 1.96):
df["Subject average"] = df.mean(axis = 1)
df_grand_avg = df["Subject average"].mean()
df["New condition1"] = df["condition1"] - df["Subject average"] + df_grand_avg
df["New condition2"] = df["condition2"] - df["Subject average"] + df_grand_avg
std_vo, std_me = df["New condition1"].std(), df["New condition2"].std()
@wiccy46
wiccy46 / bandwidth2q.py
Created July 22, 2019 16:21
[bandwidth2q]Convert frequency bandwidth to q factor. #python
# This code covert formant bandwidth to q
import sys
f0 = map(float, sys.argv[1].strip('[]').split(','))
bw = map(float, sys.argv[2].strip('[]').split(','))
q = []
if (not len(f0)==len(bw)):
print "Error: Two arguments need to have the same lenght. "
print 'Usage: First argument list of fundamental frequency, e.g "[19, 299, 449]"'
@wiccy46
wiccy46 / numpy_subset.py
Created July 19, 2019 09:44
[numpy_subset]A collection of different number subsetting #python
"""To get a new array by subsetting columns of different np.ndarray"""
a = np.array([[1,2],[3,4],[5,6]])
b = np.array([[10,20],[30,40],[50,60]])
# Using zip
[(a_s[0], b_s[0]) for a_s, b_s in zip(a,b)]
# A faster way is to concat and ravel, use ('float, float') if float number.
np.c_[a[:,0],b[:,0]].view('i,i').ravel()
@wiccy46
wiccy46 / env_detect.py
Last active July 23, 2019 08:25
[env_detec]Envelope Detections of signal #python #dsp
"""1: Peak Detection"""
def envfol_peak_detection(sig, sr, chunk=4, order=6, lpfreq=150):
"""Envelope follower based on peak detection"""
s = sig.shape[0] // chunk
new_sig = np.array_split(np.abs(sig), s)
result = []
for i in new_sig:
result.append(np.max(i))
result = np.array(result)
@wiccy46
wiccy46 / mse.py
Created July 17, 2019 13:56
[MSE]Mean square error #python
def mse(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images;
# NOTE: the two images must have the same dimension
err = np.sum((imageA - imageB) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# return the MSE, the lower the error, the more "similar"
# the two images are
return err
@wiccy46
wiccy46 / ps.md
Created July 13, 2019 08:49
[Powershell]Useful Powershell Tips #Powershell

To rename a tab where you are : $psise.CurrentPowerShellTab.DisplayName = 'Dev'

To rename a remote tab, you need to do that from a local one (where the first tab is [0] ): $psise.PowerShellTabs[1].DisplayName = 'Remote-Server01'

@wiccy46
wiccy46 / label_slider.py
Created July 8, 2019 10:26
[label_slider]Pyqt5 Label slider. This create text ticks alongside qt slider #python #qt
from PyQt5.QtGui import QPainter, QPen, QFont
from PyQt5.QtWidgets import QAbstractButton, QSlider, QWidget, QVBoxLayout, QHBoxLayout,\
QStyleOptionSlider, QStyle
from PyQt5.QtCore import Qt, QRect, QPoint
import numpy as np
class LabeledSlider(QWidget):
def __init__(self, minimum, maximum, interval=1, orientation=Qt.Horizontal,
labels=None, p0=0, parent=None):
@wiccy46
wiccy46 / load_all_csv.py
Created July 8, 2019 10:22
[load_all_csv]Load all CSV files only in a folder. #python #pandas
def get_all_data(fp):
l = os.listdir(fp)
try: l.remove(".DS_Store")
except: pass
for i, fname in enumerate(l):
temp_df = pd.read_csv(fp + fname)
if (i == 0):
r = temp_df
else:
r = r.append(temp_df)
@wiccy46
wiccy46 / timeit.py
Last active April 28, 2020 05:41
[timer_decorator]Timeit decorator or class decorator#python
import time
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)