From a477481ede0e57506e1381565c0b9c6dbe8d4ff2 Mon Sep 17 00:00:00 2001 From: Thomas Lambert <t.lambert@uliege.be> Date: Thu, 14 Dec 2023 15:19:06 +0100 Subject: [PATCH] refact(findstall): replace findpeaks by islocalmax Rather than looking for the stall point using findpeaks, simply look for the highest local maximum. Not only is this faster and cleaner, but it removes the unneeded dependency to the _Signal Processing Toolbox_. --- +af_tools/findstall.m | 30 ++++++++++-------------------- CHANGELOG.md | 9 ++++++++- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/+af_tools/findstall.m b/+af_tools/findstall.m index 7bfc8b7..cf1b8b1 100644 --- a/+af_tools/findstall.m +++ b/+af_tools/findstall.m @@ -1,6 +1,7 @@ function [alpha_s, c_l_s, c_d_s] = findstall(varargin) % FINDSTALL Finds the stall point and returns the corresponding AOA, CL and CD. - % This function uses Matlab's findpeak in order to determine the position of the stall point. + % This function looks for the highest local maximm in the CL curve to determine the position + % of the stall point. % % FINDSTALL accepts inputs in the form of a Polar structure (generated with XF2MAT) or values % for angles of attack and their associated cl and cd. While alpha and cl are required for @@ -99,22 +100,11 @@ function [alpha_s, c_l_s, c_d_s] = stallfinder(alpha, c_l, c_d) % Loop for every polar data (every column of cl) for i = 1:size(c_l, 2) - % Find stall for each polar using findpeaks - maxFound = false; - minWidth = 0; - - % Iterate in order to find only one peak (largest one should indicate the stall) - while ~maxFound && minWidth <= length(alpha(:, i)) - [~, locs] = findpeaks(c_l(:, i), 'MinPeakWidth', minWidth); - if numel(locs) == 1 - maxFound = true; - else - minWidth = minWidth + 1; - end - end + % Find all local maxima in c_l, then assume the highest one is the stall point + locMax = islocalmax(c_l(:, i)); + [~, absMaxIdx] = max(locMax); - if ~maxFound - % If findpeaks does not work + if ~any(locMax) if PLOT_POLAR_IF_ERROR figure('Name', 'Debug: Polar with no stall'); plot(rad2deg(alpha(:, i)), c_l(:, i)); @@ -124,15 +114,15 @@ function [alpha_s, c_l_s, c_d_s] = stallfinder(alpha, c_l, c_d) grid on; end error('MATLAB:findstall:noStallFound', ... - ['findpeaks did not found any stall point the polar. '... + ['The stall point could not be determined as no local maximum was detected. '... 'Please provide a polar that goes a bit after the stall.\n' ... 'See the attached plot for more details']); end - alpha_s(i) = alpha(locs, i); - c_l_s(i) = c_l(locs, i); + alpha_s(i) = alpha(absMaxIdx, i); + c_l_s(i) = c_l(absMaxIdx, i); if ~isempty(c_d) - c_d_s(i) = c_d(locs, i); + c_d_s(i) = c_d(absMaxIdx, i); end end diff --git a/CHANGELOG.md b/CHANGELOG.md index 525fd08..49853b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +## [4.2.0] - 2023-12-14 + +### Changed + +- **findstall**: use `islocalmax` rather than `findpeaks` to find stall point. + ## [4.1.0] - 2022-11-25 ### Added @@ -127,7 +133,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Initial release -[Unreleased]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v4.1.0...master +[Unreleased]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v4.2.0...master +[4.2.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/4.1.0...v4.2.0 [4.1.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/4.0.0...v4.1.0 [4.0.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/3.0.0...4.0.0 [3.0.0]: https://gitlab.uliege.be/am-dept/matlab_airfoil_toolbox/-/compare/v2.0.1...v3.0.0 -- GitLab