# How to shift a signal on the $y$-axis to match another signal in MATLAB

I have two signals recorded in the time-domain then converted to the frequency domain. These signals are $z$-axis accelerometer measurements recorded from an Arduino 101.

Now visually they are fairly similar (with the peaks and falls sort of lining up), however the difference is in their amplitude, e.g.: I will be comparing the power of the two signals, so I need them to be in-line with each other, ie. shift either one of the signals to fall around the same area.

I'm not too sure what the terminology for this is, but any help would seriously be appreciated.

• What about adding a constant value (nearly 0.15) to the green curve? – Tendero Mar 28 '17 at 17:17
• I'm trying to look for a more general solution since this is only a single test case. I did think about taking adding the difference of the average of the two and adding it to the lower signal, however I'm not sure if that will always work (the average can get a bit skewed due to some extreme values). And besides, it felt too hack-y to feel correct. :-) Is there a way to normalise the curves using some other method? – doberoi96 Mar 28 '17 at 17:25
• What about taking the average of each one and substracting it from them? The two signals would be centered at $y=0$ in that case (sort of). – Tendero Mar 28 '17 at 17:28
• Are you interested in best linear mapping where you adjust gain and offset, or are you only interested in offset? (If just offset then I believe Tendero's response is best, just also include a test to exclude samples of each waveform that are outside "N sigma" of its own variance, where N is a decision threshold to decide between bad and useful samples (for example 4 sigma should be fine). If you want to adjust gain and offset then this is a simple least squares curve fit problem. Let me know if you do want both gain and offset and I will answer that below. – Dan Boschen Mar 28 '17 at 23:48
• Hi! I was going to add an edit to my answer saying that if I need the powers of the signals to be comparable, I would need to adjust the gain as well. So yes, it would be great if you could show how to adjust the gain and offset. Thanks for your help! – doberoi96 Mar 29 '17 at 0:38

Matlab's polyfit function can be used to do linear regression. This can be used to find a linear relationship (offset and gain) that maps one dataset to the other.

• Hey @nibot, I've been trying to get it to work, but something is going wrong. Could you give an example on how exactly to use polyfit for this case? I seem to be getting two straight lines, one with an a positive slope and the other with a negative. Thanks! – doberoi96 Apr 2 '17 at 21:17

I will be comparing the power of the two signals, so I need them to be in-line with each other ...

you will be comparing what power? their total power, which is AC power + DC power?

to "shift" one of the signals along the $y$-axis, you are adding or subtracting a DC component to it.

it seems to me, that if you're comparing the powers of the two signals but need to aligned them along the $y$-axis, you must be comparing only the AC power, because you are tossing out the DC difference between them. if that is the case, seems to me the simplest thing to do is pass both signals through two identical DC-blocking highpass filters and compare the powers of the outputs of both. you get the power of a signal by squaring it and low-pass filtering the result of that.

• ooops, i just realized this is frequency-domain. that "DC-offest" in the frequency domain means that every frequency component of the red signal has more energy than that of the green signal. so doesn't this just mean the red signal has more power? if you just adjust the gain of one to match the other, you are adjusting the gross power of one to be the gross power of the other. then i do not understand what you mean by "comparing the power[s] of the two signals". – robert bristow-johnson Mar 30 '17 at 4:06
• Thanks Robert for the response. Sorry about misleading wording of the comment. I realise that the red signal will have more power. However, for the scope of the question, let's just say I need the signals to sort of align (adjust the offset) without too much disparity between their amplitude (adjust the gain). – doberoi96 Mar 30 '17 at 5:46

First you can calculate the energy of both signals and adjust them to be equal. Then maybe you can apply cross-correlation or AMDF to find the best match. Another possibility would be to apply the DTW algorithm depending on your objectives.

To give an example from the realm of audio equipment: magnitude responses are customarily normalized by offsetting the measurement so that 1000 Hz is your reference point at 0 dB (without unit since it's self-referantial). If you only want to compare the two measurements in terms of differences in their magnitude response, you could choose a frequency as your reference point (e.g. 100 Hz) and normalize both (and all future) responses for 0 dB at that frequency (so subtracting the value at the reference frequency from the whole curve). This procedure is commonly called normalization.
In order to obtain the Decibel values, just take the base-10 logarithm of the two magnitude responses and multiply by 20.