first draft for first deadline.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
display("functions")
|
||||
source("settings.m");
|
||||
|
||||
|
||||
function files = getDataFiles(clsName, trainsetPerClass)
|
||||
|
||||
global setDataDir;
|
||||
@@ -35,19 +34,21 @@ function samples = getSamplesForClass(clsName, trainsetPerClass, start, percent)
|
||||
raw = {Gyro(start:pEnd,:), Accel(start:pEnd,:), Magnet(start:pEnd,:)}; # create cell array of the wanted samples
|
||||
samples{i}.raw = raw; # write them into cell array for each Class
|
||||
end
|
||||
display(strcat("training data for '", clsName, "': ", num2str(length(samples)), " samples"));
|
||||
disp(strcat("training data for '", clsName, "': ", num2str(length(samples)), " samples"));
|
||||
end
|
||||
|
||||
function data = getRawTrainData()
|
||||
|
||||
#global trainsetPerClass;
|
||||
global signalStart;
|
||||
global signalEnd;
|
||||
|
||||
data = {};
|
||||
data{1}.samples = getSamplesForClass("forwardbend", 9, 10, 0.95);
|
||||
data{2}.samples = getSamplesForClass("kneebend", 11, 10, 0.95);
|
||||
data{3}.samples = getSamplesForClass("pushups", 11, 10, 0.95);
|
||||
data{4}.samples = getSamplesForClass("situps", 8, 10, 0.95);
|
||||
data{5}.samples = getSamplesForClass("jumpingjack", 13, 10, 0.95);
|
||||
data{1}.samples = getSamplesForClass("forwardbend", 9, signalStart, signalEnd);
|
||||
data{2}.samples = getSamplesForClass("kneebend", 11, signalStart, signalEnd);
|
||||
data{3}.samples = getSamplesForClass("pushups", 11, signalStart, signalEnd);
|
||||
data{4}.samples = getSamplesForClass("situps", 8, signalStart, signalEnd);
|
||||
data{5}.samples = getSamplesForClass("jumpingjack", 13, signalStart, signalEnd);
|
||||
end
|
||||
|
||||
function plotData(data,outPath)
|
||||
@@ -107,6 +108,12 @@ function windowedData = windowData(data)
|
||||
global setWindowSize;
|
||||
global setWindowSliding;
|
||||
windowedData = {};
|
||||
|
||||
global setMagnet;
|
||||
global setAccel;
|
||||
global setGyro;
|
||||
global useSignalPCA;
|
||||
global useSignalMG;
|
||||
|
||||
for k = 1:numel(data)
|
||||
for j = 1:numel(data{k}.samples)
|
||||
@@ -135,60 +142,68 @@ function windowedData = windowData(data)
|
||||
|
||||
for i = winBuffer:setWindowSliding:winEnd-winBuffer #steps for sliding/overlapping windows
|
||||
|
||||
#accel
|
||||
winAccel = window(data{k}.samples{j}.raw{1}, i);
|
||||
winsAccelX = [winsAccelX winAccel(:,1)];
|
||||
winsAccelY = [winsAccelY winAccel(:,2)];
|
||||
winsAccelZ = [winsAccelZ winAccel(:,3)];
|
||||
if setAccel == true
|
||||
#accel
|
||||
winAccel = window(data{k}.samples{j}.raw{1}, i);
|
||||
winsAccelX = [winsAccelX winAccel(:,1)];
|
||||
winsAccelY = [winsAccelY winAccel(:,2)];
|
||||
winsAccelZ = [winsAccelZ winAccel(:,3)];
|
||||
|
||||
if useSignalPCA == true
|
||||
[coeff1, score1] = princomp(winAccel');
|
||||
winsAccelPCA = [winsAccelPCA score1(:,1)]; #choose the first axis (eigvec with the biggest eigvalue)
|
||||
endif
|
||||
|
||||
if useSignalMG == true
|
||||
winAccelMG = getMagnitude(winAccel);
|
||||
winsAccelMG = [winsAccelMG winAccelMG];
|
||||
endif
|
||||
|
||||
endif
|
||||
|
||||
[coeff1, score1] = princomp(winAccel);
|
||||
winsAccelPCA = [winsAccelPCA score1(:,1)]; #choose the first axis (eigvec with the biggest eigvalue)
|
||||
if setGyro == true
|
||||
#gyro
|
||||
winGyro = window(data{k}.samples{j}.raw{2}, i);
|
||||
winsGyroX = [winsGyroX winGyro(:,1)];
|
||||
winsGyroY = [winsGyroY winGyro(:,2)];
|
||||
winsGyroZ = [winsGyroZ winGyro(:,3)];
|
||||
|
||||
if useSignalPCA == true
|
||||
[coeff2, score2] = princomp(winGyro');
|
||||
winsGyroPCA = [winsGyroPCA score2(:,1)];
|
||||
endif
|
||||
|
||||
if useSignalMG == true
|
||||
winGyroMG = getMagnitude(winGyro);
|
||||
winsGyroMG = [winsGyroMG winGyroMG];
|
||||
endif
|
||||
|
||||
endif
|
||||
|
||||
winAccelMG = getMagnitude(winAccel);
|
||||
winsAccelMG = [winsAccelMG winAccelMG];
|
||||
|
||||
#gyro
|
||||
winGyro = window(data{k}.samples{j}.raw{2}, i);
|
||||
winsGyroX = [winsGyroX winGyro(:,1)];
|
||||
winsGyroY = [winsGyroY winGyro(:,2)];
|
||||
winsGyroZ = [winsGyroZ winGyro(:,3)];
|
||||
|
||||
[coeff2, score2] = princomp(winGyro);
|
||||
winsGyroPCA = [winsGyroPCA score2(:,1)];
|
||||
|
||||
winGyroMG = getMagnitude(winGyro);
|
||||
winsGyroMG = [winsGyroMG winGyroMG];
|
||||
|
||||
#magnet
|
||||
winMagnet = window(data{k}.samples{j}.raw{3}, i);
|
||||
winsMagnetX = [winsMagnetX winMagnet(:,1)];
|
||||
winsMagnetY = [winsMagnetY winMagnet(:,2)];
|
||||
winsMagnetZ = [winsMagnetZ winMagnet(:,3)];
|
||||
|
||||
[coeff3, score3] = princomp(winMagnet);
|
||||
winsMagnetPCA = [winsMagnetPCA score3(:,1)];
|
||||
|
||||
winMagnetMG = getMagnitude(winMagnet);
|
||||
winsMagnetMG = [winsMagnetMG winMagnetMG];
|
||||
if setMagnet == true
|
||||
#magnet
|
||||
winMagnet = window(data{k}.samples{j}.raw{3}, i);
|
||||
winsMagnetX = [winsMagnetX winMagnet(:,1)];
|
||||
winsMagnetY = [winsMagnetY winMagnet(:,2)];
|
||||
winsMagnetZ = [winsMagnetZ winMagnet(:,3)];
|
||||
|
||||
if useSignalPCA == true
|
||||
[coeff3, score3] = princomp(winMagnet');
|
||||
winsMagnetPCA = [winsMagnetPCA score3(:,1)];
|
||||
endif
|
||||
|
||||
if useSignalMG == true
|
||||
winMagnetMG = getMagnitude(winMagnet);
|
||||
winsMagnetMG = [winsMagnetMG winMagnetMG];
|
||||
endif
|
||||
|
||||
endif
|
||||
|
||||
end
|
||||
|
||||
#write back data
|
||||
windowedData{k}.samples{j}.raw{1}.wins = winsAccelX; #X
|
||||
windowedData{k}.samples{j}.raw{2}.wins = winsAccelY; #Y
|
||||
windowedData{k}.samples{j}.raw{3}.wins = winsAccelZ; #Z
|
||||
windowedData{k}.samples{j}.raw{4}.wins = winsGyroX;
|
||||
windowedData{k}.samples{j}.raw{5}.wins = winsGyroY;
|
||||
windowedData{k}.samples{j}.raw{6}.wins = winsGyroZ;
|
||||
windowedData{k}.samples{j}.raw{7}.wins = winsMagnetX;
|
||||
windowedData{k}.samples{j}.raw{8}.wins = winsMagnetY;
|
||||
windowedData{k}.samples{j}.raw{9}.wins = winsMagnetZ;
|
||||
windowedData{k}.samples{j}.raw{10}.wins = winsAccelPCA;
|
||||
windowedData{k}.samples{j}.raw{11}.wins = winsGyroPCA;
|
||||
windowedData{k}.samples{j}.raw{12}.wins = winsMagnetPCA;
|
||||
windowedData{k}.samples{j}.raw{13}.wins = winsAccelMG;
|
||||
windowedData{k}.samples{j}.raw{14}.wins = winsGyroMG;
|
||||
windowedData{k}.samples{j}.raw{15}.wins = winsMagnetMG;
|
||||
|
||||
#write back data
|
||||
windowedData{k}.samples{j}.raw = [winsAccelX; winsAccelY; winsAccelZ; winsGyroX; winsGyroY; winsGyroZ; winsMagnetX; winsMagnetY; winsMagnetZ; winsAccelPCA; winsGyroPCA; winsMagnetPCA; winsAccelMG; winsGyroMG; winsMagnetMG];
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -198,44 +213,66 @@ function features = featureCalculation(data)
|
||||
global setAutocorrelationBinSize;
|
||||
global setPSDBinSize;
|
||||
features = [];
|
||||
|
||||
global setAutoCorr;
|
||||
global setRMS;
|
||||
global setPSD;
|
||||
global setStatistic;
|
||||
global setPCA;
|
||||
|
||||
for k = 1:numel(data)
|
||||
for j = 1:numel(data{k}.samples)
|
||||
for i = 1:numel(data{k}.samples{j}.raw)
|
||||
for m = 1:numel(data{k}.samples{j}.raw{i}.wins)
|
||||
currentWindow = data{k}.samples{j}.raw{i}.wins{m};
|
||||
for i = 1:size(data{k}.samples{j}.raw, 1)
|
||||
for m = 1:size(data{k}.samples{j}.raw, 2)
|
||||
currentWindow = data{k}.samples{j}.raw{i, m};
|
||||
|
||||
#autocorrelation on window. split into 5 evenly spaced bins (frequencies are evenly spaced, not number of values ;) ) and calculate mean of bin.
|
||||
[autoCorr] = xcorr(currentWindow);
|
||||
[binNum, binCenter] = hist(autoCorr, setAutocorrelationBinSize); #define bins for the data.
|
||||
binSize = abs(binCenter(end-1) - binCenter(end));
|
||||
binEdges = linspace(binCenter(1)-(binSize/2), binCenter(end)+(binSize/2), setAutocorrelationBinSize+1);
|
||||
[binNumc, binIdx] = histc(autoCorr, binEdges);
|
||||
binMeans = getBinMean(autoCorr, binIdx, setAutocorrelationBinSize);
|
||||
if setAutoCorr == true
|
||||
#autocorrelation on window. split into 5 evenly spaced bins (amplitudes are evenly spaced, not number of values ;) ) and calculate mean of bin.
|
||||
[autoCorr] = xcorr(currentWindow);
|
||||
[binNum, binCenter] = hist(autoCorr, setAutocorrelationBinSize); #define bins for the data.
|
||||
binSize = abs(binCenter(end-1) - binCenter(end));
|
||||
binEdges = linspace(binCenter(1)-(binSize/2), binCenter(end)+(binSize/2), setAutocorrelationBinSize+1);
|
||||
[binNumc, binIdx] = histc(autoCorr, binEdges);
|
||||
binMeans = getBinMean(autoCorr, binIdx, setAutocorrelationBinSize);
|
||||
|
||||
endif
|
||||
|
||||
#calculate the root-mean-square (RMS) of the signal
|
||||
rms = sqrt(mean(currentWindow.^2));
|
||||
if setRMS == true
|
||||
#calculate the root-mean-square (RMS) of the signal
|
||||
rms = sqrt(mean(currentWindow.^2));
|
||||
endif
|
||||
|
||||
if setPSD == true
|
||||
#power bands 0.5 to 25hz (useful if the windows are greater then 4s and window sizes to 256, 512..)
|
||||
[powerBand, w] = periodogram(currentWindow); #fills up fft with zeros
|
||||
powerEdges = logspace(log10(0.5), log10(25), setPSDBinSize + 2); #logarithmic bin spaces for 10 bins
|
||||
triFilter = getTriangularFunction(powerEdges, length(powerBand)*2 - 2);
|
||||
for l = 1:numel(triFilter)
|
||||
filteredBand = triFilter{l} .* powerBand;
|
||||
psd(l) = sum(filteredBand); #sum freq (no log and no dct)
|
||||
end
|
||||
endif
|
||||
|
||||
if setStatistic == true
|
||||
#statistical features
|
||||
windowMean = mean(currentWindow);
|
||||
windowSTD = std(currentWindow); #standard deviation
|
||||
windowVariance = var(currentWindow);
|
||||
windowKurtosis = kurtosis(currentWindow); #(ger. Wölbung)
|
||||
windowIQR = iqr(currentWindow); #interquartile range
|
||||
endif
|
||||
|
||||
#power bands 0.5 to 25hz (useful if the windows are greater then 4s and window sizes to 256, 512..)
|
||||
[powerBand, w] = periodogram(currentWindow); #fills up fft with zeros
|
||||
powerEdges = logspace(log10(0.5), log10(25), setPSDBinSize + 2); #logarithmic bin spaces for 10 bins
|
||||
triFilter = getTriangularFunction(powerEdges, length(powerBand)*2 - 2);
|
||||
for l = 1:numel(triFilter)
|
||||
filteredBand = triFilter{l} .* powerBand;
|
||||
psd(l) = sum(filteredBand); #sum freq (no log and no dct)
|
||||
end
|
||||
|
||||
#statistical features
|
||||
windowMean = mean(currentWindow);
|
||||
windowSTD = std(currentWindow); #standard deviation
|
||||
windowVariance = var(currentWindow);
|
||||
windowKurtosis = kurtosis(currentWindow); #(ger. Wölbung)
|
||||
windowIQR = iqr(currentWindow); #interquartile range
|
||||
pcaFeature = [];
|
||||
if setPCA == true
|
||||
#pca on windows
|
||||
[coeff, score] = princomp(currentWindow');
|
||||
pcaFeature = currentWindow' * coeff(:, 1:10); #10 pca feature
|
||||
endif
|
||||
|
||||
#put everything together
|
||||
classLabel = k; #what class?
|
||||
sampleLabel = j; #what sample?
|
||||
features = [features; sampleLabel, classLabel, binMeans, rms, psd, windowMean, windowSTD, windowVariance, windowKurtosis, windowIQR];
|
||||
features = [features; sampleLabel, classLabel, binMeans, rms, psd, windowMean, windowSTD, windowVariance, windowKurtosis, windowIQR, pcaFeature];
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -258,7 +295,7 @@ function value = getBinMean(data, idx, numBins)
|
||||
if length(binMembers) == 0
|
||||
value(i) = 0;
|
||||
else
|
||||
value(i) = mean(binMembers);
|
||||
value(i) = sum(binMembers);
|
||||
endif
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user