Skip to content

Commit

Permalink
Added freq alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
darrencl committed Mar 16, 2020
1 parent 051c672 commit f0f49bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/MagneticResonanceSignals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export
hsvd_water_suppression,
# Phase correction
ernst,
adjust_phase
adjust_phase,
# Frequency alignment
align_frequency!

# High level signal processing
export
Expand Down
28 changes: 28 additions & 0 deletions src/processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,31 @@ function baseline_als(spectrum::AxisArray, lambda::Float64, p::Float64; niter::I
AxisArrays.axes(y)
)
end

"""
align_frequency!(spectrums::AbstractVector)
Align a list of spectrums in a highest peak with first spectrum as a reference. This will
pad the spectrum with 0s after shifting to retain the original length.
"""
function align_frequency!(spectrums::AbstractVector)
# Use first spectrum as a reference
highest_peak_ref_idx = findmax(spectrums[1])[2]
original_length = length(spectrums[1])
for idx=2:length(spectrums)
highest_peak_idx = findmax(spectrums[idx])[2]
shift = highest_peak_idx - highest_peak_ref_idx
pads = [0 for i=1:abs(shift)]
if shift > 0
# Shift left: right-pad
push!(spectrums[idx], pads...)
spectrums[idx] = spectrums[idx][abs(shift)+1:abs(shift)+original_length]
nothing
elseif shift < 0
# Shift right: left-pad
pushfirst!(spectrums[idx], pads...)
spectrums[idx] = spectrums[idx][1:end-abs(shift)]
nothing
end
end
end
15 changes: 15 additions & 0 deletions test/processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,18 @@ end
@test !(spec_base[1] abs.(spec)[1])
@test spec_base[1] 2.350083f-8
end

@testset "frequency alignment" begin
test_data = [
[1, 2, 4, 10, 2, 4, 3],
[1, 2, 50, 2, 4, 2, 3],
[1, 3, 4, 5, 10, 1, 2]
]
align_frequency!(test_data)
expected = [
[1, 2, 4, 10, 2, 4, 3],
[0, 1, 2, 50, 2, 4, 2],
[3, 4, 5, 10, 1, 2, 0]
]
@test test_data == expected
end

0 comments on commit f0f49bf

Please sign in to comment.