Skip to content

Instantly share code, notes, and snippets.

View stormxuwz's full-sized avatar
🤣
Sleepy?

Wenzhao Xu stormxuwz

🤣
Sleepy?
View GitHub Profile
@stormxuwz
stormxuwz / mfcc.py
Created February 2, 2015 05:17
MFCC for signal
from __future__ import division
import numpy as np;
import matplotlib.pyplot as plt
import warnings
# Ref: http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/
# Ref2:http://python-speech-features.readthedocs.org/en/latest/. However, I checked the library code and found there might be a mistake when using rfft
def preemphasis(signal,coeff=0.95):
return numpy.append(signal[0],signal[1:]-coeff*signal[:-1])
@stormxuwz
stormxuwz / dft.py
Created February 2, 2015 05:21
This is a Discrete Fourier Transformation calculation using matrix mutiplication
def omega(N,k,n):
return np.exp(-2*np.pi/N*k*n*1j)
def hanWindow(N):
diag=np.array([np.sin(np.pi*i/(N-1))**2 for i in range(N)])
H=np.diag(diag)
return H
def createF(N):
F=np.zeros((N,N),dtype=complex)
@stormxuwz
stormxuwz / VAR.py
Created February 25, 2015 06:30
Activity recognition by VAR model (HAR dataset)
import statsmodels.tsa as st
import numpy as np
import numpy.linalg as nl
import statsmodels.tsa as tsa
def solver_leastSquare(A,y):
return nl.lstsq(A,y);
def read_data(filename):
x=[]
@stormxuwz
stormxuwz / three_dots.R
Created March 12, 2015 20:00
The three-dots construct in R
test_function_1 <- function(a=1){
cat("a is here",a)
}
test_function_2 <- function(b,...){
test_function_1(...)
}
test <- function(a,b){
test_function_2(b,a)
@stormxuwz
stormxuwz / onlineClf.py
Created November 18, 2015 01:05
some online algorithm
from __future__ import division
import numpy as np
class OnlineClf(object):
def __init__(self,iterNum,R):
self.w=None
self.iterNum=iterNum
self.misNum=[[0,0]]
@stormxuwz
stormxuwz / ts_seg.py
Created February 25, 2016 21:09
time series segmentation
import numpy as np
def slidingWindow(x,max_error):
n=len(x)
leftNode=0
segmentList=[]
print n
while leftNode<n-1:
print leftNode
from __future__ import division
import numpy as np
import chainer # only for import the data at this script
def softmax(z):
# z is a vector
return np.exp(z) / np.sum(np.exp(z))
def sigmoid(x):
randomForest_clf <- function(matTrain,trainLabel,matTest,...){
library(randomForest)
rf_model<-randomForest(matTrain,trainLabel,ntree=500)
rf_predict<-predict(rf_model,matTest)
return(rf_predict)
}
xgboost_clf_1000 <- function(matTrain,trainLabel,matTest,...){
library(xgboost)
@stormxuwz
stormxuwz / classfication_method.R
Created September 22, 2016 03:12
Music information retrieve for music classification
## Classification starts now
library(MASS)
library(e1071)
library(rda)
####################################
qda.model=function(traindata){
qda.result=qda(Y~.,data=traindata)
return(qda.result)
}
# This is a pure python K-means implementation
import numpy as np
def calDistance(x,y):
# return the distance of x and y
return np.sum((x-y)**2)
def assignClusters(centers,data):
distance = np.zeros((len(data),len(centers)))