Last active
May 10, 2021 01:20
-
-
Save awade/5ec3884b60675ddccb75f594116d8699 to your computer and use it in GitHub Desktop.
Matlab function for computing the rms integrating from high frequency to low frequency. Has additional option to clip the integration to some frequency upper bound.
This file contains hidden or 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
function [x_clipped, rms] = rms2(x, y, varargin) | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
% | |
% find the rms value of y(x) | |
% y(f) could be a spectral density | |
% or y(t) could be a time series | |
% | |
% optional var sets the cutoff of x to compute | |
% from. This will shorten the output vector | |
% and returns x_clipped, yRMS intergrating from | |
% top to bottom | |
% | |
% usage: rmsY = rms(X,Y); | |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
% Assert and handle input because matlab is terrible | |
if isempty(varargin) | |
f_upperB = max(x); | |
elseif length(varargin) == 1 | |
f_upperB = varargin{1}; | |
else | |
error("Too many optional argments passed in") | |
end | |
% Assert x and y are vectors with some length longer one form nx1 or 1xm | |
if ~xor((size(x,1)==1),(size(x,2)==1)) | |
error("Input data x must be vector") | |
end | |
if ~xor((size(y,1)==1),(size(y,2)==1)) | |
error("Input data y must be vector") | |
end | |
% Handle translation of input vectors to form 1xm | |
if size(x, 2) == 1 | |
x = x'; | |
trasposeOutput_x = true; % Remember to tranpose on output | |
else | |
trasposeOutput_x = false; | |
end | |
if size(y, 2) == 1 | |
y = y'; | |
trasposeOutput_y = true; % Remember to tranpose on output | |
else | |
trasposeOutput_y = false; | |
end | |
% Apply clipping of x range, if given by input, else default to full range | |
if (max(x)>f_upperB) | |
x_clipped = x(1:find(x > f_upperB, 1)); | |
y_clipped = y(1:find(x > f_upperB, 1)); | |
else | |
x_clipped = x; | |
y_clipped = y; | |
end | |
dx = diff(squeeze(x_clipped)); | |
dx = [x_clipped(1) dx]; | |
rms = fliplr(sqrt(cumsum(fliplr((squeeze(y_clipped).^2.*dx))))); | |
% Transpose outputs to match input | |
if trasposeOutput_x | |
x_clipped = x_clipped'; | |
end | |
if trasposeOutput_y | |
rms = rms'; | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment