-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds the upcrossing analysis functions in MATLAB #151
Open
MShabara
wants to merge
4
commits into
MHKiT-Software:develop
Choose a base branch
from
MShabara:upcrossing
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
classdef upcrossing_Test < matlab.unittest.TestCase | ||
properties | ||
t | ||
signal | ||
zeroCrossApprox | ||
end | ||
|
||
methods (TestClassSetup) | ||
% Shared setup for the entire test class | ||
function setupTestClass(testCase) | ||
% Define time vector | ||
testCase.t = linspace(0, 4, 1000); | ||
|
||
% Define signal | ||
testCase.signal = testCase.exampleWaveform_(testCase.t); | ||
|
||
% Approximate zero crossings | ||
testCase.zeroCrossApprox = [0, 2.1, 3, 3.8]; | ||
end | ||
|
||
end | ||
|
||
methods (TestMethodSetup) | ||
% Setup for each test | ||
end | ||
|
||
% methods (Test) | ||
% % Test methods | ||
% | ||
% function unimplementedTest(testCase) | ||
% testCase.verifyFail("Unimplemented test"); | ||
% end | ||
% end | ||
|
||
methods | ||
function signal = exampleWaveform_(~, t) | ||
% Generate a simple waveform form to analyse | ||
% This has been created to perform | ||
% a simple independent calcuation that | ||
% the mhkit functions can be tested against. | ||
A = [0.5, 0.6, 0.3]; | ||
T = [3, 2, 1]; | ||
w = 2 * pi ./ T; | ||
|
||
signal = zeros(size(t)); | ||
for i = 1:length(A) | ||
signal = signal + A(i) * sin(w(i) * t); | ||
end | ||
end | ||
|
||
function [crests, troughs, heights, periods] = exampleAnalysis_(testCase, signal) | ||
% NB: This only works due to the construction | ||
% of our test signal. It is not suitable as | ||
% a general approach. | ||
|
||
% Gradient-based turning point analysis | ||
grad = diff(signal); | ||
|
||
% +1 to get the index at turning point | ||
turningPoints = find(grad(1:end-1) .* grad(2:end) < 0) + 1; | ||
|
||
crestInds = turningPoints(signal(turningPoints) > 0); | ||
troughInds = turningPoints(signal(turningPoints) < 0); | ||
|
||
crests = signal(crestInds); | ||
troughs = signal(troughInds); | ||
heights = crests - troughs; | ||
|
||
% Numerical zero-crossing solution | ||
zeroCross = zeros(size(testCase.zeroCrossApprox)); | ||
for i = 1:length(testCase.zeroCrossApprox) | ||
zeroCross(i) = fzero(@(x) testCase.exampleWaveform_(x), ... | ||
testCase.zeroCrossApprox(i)); | ||
end | ||
|
||
periods = diff(zeroCross); | ||
end | ||
end | ||
|
||
methods (Test) | ||
%% Test functions without indices (inds) | ||
function test_peaks(testCase) | ||
[want, ~, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
got = uc_peaks(testCase.t, testCase.signal); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_troughs(testCase) | ||
[~, want, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
got = uc_troughs(testCase.t, testCase.signal); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_heights(testCase) | ||
[~, ~, want, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
got = uc_heights(testCase.t, testCase.signal); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_periods(testCase) | ||
[~, ~, ~, want] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
got = uc_periods(testCase.t, testCase.signal); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 2e-3); | ||
end | ||
|
||
function test_custom(testCase) | ||
[want, ~, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
% create a similar function to finding the peaks | ||
f = @(ind1, ind2) max(testCase.signal(ind1:ind2)); | ||
|
||
got = uc_custom(testCase.t, testCase.signal, f); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
%% Test functions with indcies | ||
function test_peaks_with_inds(testCase) | ||
[want, ~, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
inds = upcrossing(testCase.t, testCase.signal); | ||
|
||
got = uc_peaks(testCase.t, testCase.signal, inds); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_trough_with_inds(testCase) | ||
[~, want, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
inds = upcrossing(testCase.t, testCase.signal); | ||
|
||
got = uc_troughs(testCase.t, testCase.signal, inds); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_heights_with_inds(testCase) | ||
[~, ~, want, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
|
||
inds = upcrossing(testCase.t, testCase.signal); | ||
|
||
got = uc_heights(testCase.t, testCase.signal, inds); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
function test_periods_with_inds(testCase) | ||
[~, ~, ~, want] = testCase.exampleAnalysis_(testCase.signal); | ||
inds = upcrossing(testCase.t, testCase.signal); | ||
|
||
got = uc_periods(testCase.t, testCase.signal,inds); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 2e-3); | ||
end | ||
|
||
function test_custom_with_inds(testCase) | ||
[want, ~, ~, ~] = testCase.exampleAnalysis_(testCase.signal); | ||
inds = upcrossing(testCase.t, testCase.signal); | ||
|
||
% create a similar function to finding the peaks | ||
f = @(ind1, ind2) max(testCase.signal(ind1:ind2)); | ||
|
||
got = uc_custom(testCase.t, testCase.signal, f, inds); | ||
|
||
testCase.verifyEqual(got, want, 'AbsTol', 1e-3); | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this code?