new general processing structure just before splitting off the projects folder from this repo
80 lines
2.6 KiB
Matlab
80 lines
2.6 KiB
Matlab
function [std_total,std_lvl] = calc_std(test_signal,reference_signal,options)
|
|
|
|
arguments(Input)
|
|
test_signal;
|
|
reference_signal;
|
|
options.skip_front = 0;
|
|
options.skip_end = 0;
|
|
options.returnErrorLocation = 0;
|
|
end
|
|
|
|
options.skip_end = abs(options.skip_end);
|
|
options.skip_front = abs(options.skip_front);
|
|
|
|
assert((options.skip_end+options.skip_front)<length(test_signal),"You can not skip more bits than overall length of data! Set skip_front or skip_end to lower value or check data_in");
|
|
|
|
if isa(reference_signal,'Signal')
|
|
reference_signal = reference_signal.signal;
|
|
end
|
|
|
|
if isa(test_signal,'Signal')
|
|
test_signal = test_signal.signal;
|
|
end
|
|
|
|
% TRIM
|
|
[test_signal,reference_signal]=trimseq(test_signal,reference_signal,options.skip_front,options.skip_end);
|
|
|
|
% CALC EVM
|
|
[std_total, std_lvl, nsd_lvl, d_min]= calc_std_(test_signal,reference_signal);
|
|
std_lvl = nsd_lvl;
|
|
|
|
function [std_total, std_lvl, nsd_lvl, d_min] = calc_std_(test_signal, reference_signal)
|
|
|
|
% NSD (Normalized Standard Deviation) expresses the noise spread at each symbol level
|
|
% relative to the minimum distance between levels. A low NSD means low error risk,
|
|
% while NSD approaching 0.5 indicates a high chance of symbol errors due to noise.
|
|
|
|
% Ensure input is column vector
|
|
test_signal = test_signal(:);
|
|
reference_signal = reference_signal(:);
|
|
|
|
assert(length(test_signal) == length(reference_signal), "Sequence length does not match");
|
|
|
|
% Find unique levels and their minimum spacing
|
|
k = unique(reference_signal);
|
|
d_min = min(diff(k)); % Minimum distance between adjacent levels
|
|
|
|
% Normalize test signal to RMS=1 (if not already)
|
|
test_signal = test_signal / rms(test_signal);
|
|
|
|
% Overall standard deviation (not normalized)
|
|
std_total = std(test_signal);
|
|
|
|
% Per-level standard deviation and normalized std
|
|
std_lvl = zeros(1, length(k));
|
|
nsd_lvl = zeros(1, length(k));
|
|
for lvl = 1:length(k)
|
|
idx = reference_signal == k(lvl);
|
|
if any(idx)
|
|
std_lvl(lvl) = std(test_signal(idx));
|
|
nsd_lvl(lvl) = std_lvl(lvl) / d_min;
|
|
else
|
|
std_lvl(lvl) = NaN;
|
|
nsd_lvl(lvl) = NaN;
|
|
end
|
|
end
|
|
end
|
|
|
|
function [data_,reference_] = trimseq(data,reference,skipstart,skip_end)
|
|
|
|
data_ = data(skipstart+1:end-skip_end,:);
|
|
|
|
delta_bits = length(reference) - length(data);
|
|
|
|
skip_end = delta_bits + skip_end;
|
|
|
|
reference_ = reference(skipstart+1:end-skip_end,:);
|
|
|
|
end
|
|
|
|
end |