Commit 4c2d3088 by Julien Audiffren

Added SWARII for python 2.7

parent 9913010d
# -*- coding: utf-8 -*-
"""
Copyright 2016 Julien Audiffren
This code is related to the paper :
[Preprocessing of the Nintendo Wii Board Signal to Derive More
Accurate Descriptors of Statokinesigrams, Audiffren and Contal]
If you use this algorithm in your research work, please cite this paper.
Licence :
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from resampling import SWARII
import pylab as plb
import numpy as np
def parse_wbb_file(file_adress):
time = []
signal = []
f = open(file_adress,'rt+')
l = f.readline()
l = f.readline()
while len(l)>0:
data = l.split(" ")
t = 0.001*float(data[0])
x = float(data[1])
y = float(data[2])
time.append(t)
signal.append([x,y])
l=f.readline()
return np.array(time),np.array(signal)
if __name__=='__main__':
resampling_method = SWARII(window_size=0.25,desired_frequency=25)
time,signal = parse_wbb_file('example_wbb_statokinesigram.txt')
resampled_time, resampled_signal = resampling_method.resample(time,signal)
fig,ax = plb.subplots(2)
ax[0].plot(time,signal[:,0],c='b',label='original signal')
ax[0].plot(resampled_time,resampled_signal[:,0],c='r',label='resampled signal')
ax[0].set_xlabel("time")
ax[0].set_ylabel("X coordinate")
ax[0].set_title("X")
ax[0].legend()
ax[1].plot(time,signal[:,1],c='b',label='original signal')
ax[1].plot(resampled_time,resampled_signal[:,1],c='r',label='resampled signal')
ax[1].set_xlabel("time")
ax[1].set_ylabel("Y coordinate")
ax[1].set_title("Y")
ax[1].legend()
plb.show()
This source diff could not be displayed because it is too large. You can view the blob instead.
# -*- coding: utf-8 -*-
"""
Copyright 2016 Julien Audiffren
This code is related to the paper :
[Preprocessing of the Nintendo Wii Board Signal to Derive More
Accurate Descriptors of Statokinesigrams, Audiffren and Contal]
If you use this algorithm in your research work, please cite this paper.
Licence :
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
class SWARII:
"""
Implementation of the Sliding Windows Weighted Averaged Interpolation method
How To use :
First instantiate the class with the desired parameters
Then call resample on the desired signal
"""
def __init__(self, window_size=1, desired_frequency=25):
"""
Instantiate SWARII
Parameters :
desired_frequency : The frequency desired for the output signal,
after the resampling.
window_size : The size of the sliding window, in seconds.
"""
self.desired_frequency = desired_frequency
self.window_size = window_size
def resample(self, time, signal):
"""
Apply the SWARII to resample a given signal.
Input :
time: The time stamps of the the data point in the signal. A 1-d
array of shape n, where n is the number of points in the
signal. The unit is seconds.
signal: The data points representing the signal. A k-d array of
shape (n,k), where n is the number of points in the signal,
and k is the dimension of the signal (e.g. 2 for a
statokinesigram).
Output:
resampled_time : The time stamps of the signal after the resampling
resampled_signal : The resampled signal.
"""
a_signal=np.array(signal)
current_time = max(0.,time[0])
output_time=[]
output_signal = []
while current_time < time[-1]:
relevant_times = [t for t in range(len(time)) if abs(
time[t] - current_time) < self.window_size * 0.5]
assert len(relevant_times) > 0,"Trying to interpolate an empty window !"
if len(relevant_times) == 1:
value = a_signal[relevant_times[0]]
else :
value = 0
weight = 0
for i, t in enumerate(relevant_times):
if i == 0 or t==0:
left_border = max(
time[0], (current_time - self.window_size * 0.5))
else:
left_border = 0.5 * (time[t] + time[t - 1])
if i == len(relevant_times) - 1:
right_border = min(
time[-1], current_time + self.window_size * 0.5)
else:
right_border = 0.5 * (time[t + 1] + time[t])
w = right_border - left_border
value += a_signal[t] * w
weight += w
value /= weight
output_time.append(current_time)
output_signal.append(value)
current_time += 1. / self.desired_frequency
return np.array(output_time),np.array(output_signal)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment