Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions mhkit/wave/resource/frequency_moment.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
%
% Calculates the Nth frequency moment of the spectrum
%
% Parameters
% -----------
% S: structure or numeric array
% If structure:
% S.spectrum - Spectral density [m^2/Hz]
% S.frequency - Frequency [Hz]
% If numeric:
% S is spectral density array (vector or matrix)
% varargin{1} must contain frequency vector
%
% N: int
% Moment (0 for 0th, 1 for 1st ....)
% Computes: m_N = sum(f^N * S(f) * df) per Eq 8 in IEC 62600-101 Ed. 2.0 en 2024
%
% frequency_bins: vector (optional)
% Bin widths for frequency of S. Required for unevenly sized bins
% Parameters
% ------------
% S : struct or numeric
% If struct:
% S.spectrum : vector or matrix [m^2/Hz]
% Spectral density
% S.frequency : vector [Hz]
% Frequency
% If numeric: spectral density array, varargin{1} must be frequency
% N : integer
% Moment order (0 for 0th, 1 for 1st, -1 for inverse, etc.)
% frequency_bins : vector (optional)
% Bin widths for frequency of S. Required for unevenly sized bins.
%
% Returns
% -------
% m: double
% Nth Frequency Moment indexed by S.columns
% ---------
% m : double or vector
% Nth frequency moment. Returns a vector if S.spectrum is a matrix.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down Expand Up @@ -63,6 +63,8 @@
end

% Calculate Nth moment: m_N = sum(f^N * S * df)
% Return as column vector (MATLAB convention for time series)
m = sum((frequency.^N) .* spectrum .* freq_bins, 1);
m = m(:);

end
Loading