dsp: move duplicated code to a helper function