From 759e89f44eb6f60d8086551742263890e21c80e7 Mon Sep 17 00:00:00 2001 From: Kwok-shing Chan Date: Tue, 11 Oct 2022 21:38:46 +0200 Subject: [PATCH] bug fix for R2* --- utils/ARLO.m | 54 ++++++++++++++++++++++++++++++++++++++++++++ utils/computeFiter.m | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 utils/ARLO.m create mode 100755 utils/computeFiter.m diff --git a/utils/ARLO.m b/utils/ARLO.m new file mode 100644 index 0000000..d2f9c0f --- /dev/null +++ b/utils/ARLO.m @@ -0,0 +1,54 @@ +%% function u = ARLO(y,x) +% +% Description: Fast monoexponential fitting by auto-regression +% Samples have to be evenly sampled (i.e. even spacing) +% At least 3 samples are needed +% Assuming time series in the last dimension +% ref: Pei et al. MRM 73:843-850(2015) +% e.g. for function y=exp(-x/u), u can be estimated by ARLO +% +% Kwok-shing Chan @ DCCN +% k.chan@donders.ru.nl +% Date created: 8 October, 2016 +% Date last modified: +% +function u = ARLO(y,x) +% ensure y is real +% y=abs(y); + +% get dimension of y +ndim = ndims(y); +if ndim==2 && size(y,2)==1 + y = permute(y,[2 1]); +end +matrixSize = size(y); +N = matrixSize(end); + +% reshape y s.t. new y = [all y, time] +y = reshape(y,[numel(y)/matrixSize(end)],N); + +% get the spacing +dx = x(2)-x(1); + +% get sum of signal for i and delta i +Si = zeros(size(y,1),N-2); +deltai = zeros(size(y,1),N-2); +for k=1:N-2 + [Si(:,k), deltai(:,k)]= Simpson(y(:,k:k+2),dx); +end + +% analytical solution for minimiser to obatain u +a = sum(Si.^2,2); +b = sum(Si.*deltai,2); +u = (a + (dx/3)*b)./((dx/3)*a + b); + +% reshape u based on input dimension +u = reshape(u,[matrixSize(1:end-1)]); + +end + +% Quadratic approximation of Simpson rule's in 4th order accuracy when J=2 +function [Si, deltai] = Simpson(y,dx) +Si = (dx/3) * (y(:,1) + 4*y(:,2) + y(:,3)); +deltai = y(:,1)-y(:,3); +end \ No newline at end of file diff --git a/utils/computeFiter.m b/utils/computeFiter.m new file mode 100755 index 0000000..71eb649 --- /dev/null +++ b/utils/computeFiter.m @@ -0,0 +1,41 @@ +%% function fiter = computeFiter(s,shat,NUM_MAGN) +% s - measured signal +% shat - simulated signal +% NUM_GAGN - no. of phase corrupted echoes: +% NUM_MAGN=0 : complex fitting +% NUM_MAGN=length(s) : magnitude fitting +% NUM_MAGN (0,length(s)) : mixed fitting +% +% Description: Compute the fitter for lsqnonlin +% +% Kwok-shing Chan @ DCCN +% k.chan@donders.ru.nl +% Date created: +% Date last modified: +% +function fiter = computeFiter(s,shat,NUM_MAGN) +if NUM_MAGN == length(s) % Magnitude fitting + shat1 = abs(shat); + s1 = abs(s); + fiter = shat1(:) - s1(:); +elseif NUM_MAGN == 0 % Complex fitting + fiter2 = shat(:) - s(:); + fiter2 = [real(fiter2); imag(fiter2)]; +% fiter2 = [real(fiter2), imag(fiter2)]; + fiter = fiter2; +% fiter = abs(fiter2); +else + % Compute mixed fitting fit error + shat1 = abs(shat(1:NUM_MAGN)); + s1 = abs(s(1:NUM_MAGN)); + shat2 = shat(NUM_MAGN+1:end); + s2 = s(NUM_MAGN+1:end); + + fiter1 = shat1(:) - s1(:); + fiter2 = shat2(:) - s2(:); + fiter2 = [real(fiter2); imag(fiter2)]; + + fiter = [fiter1;fiter2]; +end +fiter = double(fiter); +end \ No newline at end of file