13
$\begingroup$

I want to compare two signals or curves. a curve sampled at 30 Hz a curve samplet at 2000 Hz

Unfortunately they have different sample rates. The first one is sampled at 30 Hz, the second at 2000 Hz. Matlab has the function 'resample' and I thought, this would make comparison much easier.

My question is: Is it wiser to downsample the second curve or to upsample the first one?

Edit: I did as I was told. In the picture on the left are the original curves. The curves on the right are resampled. Top right is upsampled, bottom right is downsampled.

I know the curves are of different length, when resampled. However, the second signal will get cropped to the first signals length. comparison

$\endgroup$
  • 1
    $\begingroup$ I upvoted your question. Which do you think is wiser. $\endgroup$ – Stanley Pawlukiewicz Nov 20 '17 at 2:13
  • $\begingroup$ if very high precision doesn't matter (in this case I think it doesn't) then you can downsample, especially if it makes the process a lot faster and you value your time at work $\endgroup$ – user1306322 Nov 20 '17 at 2:58
  • $\begingroup$ @Stanley Pawlukiewicz, clearly, I lose all the small peaks by downsampling. So upsampling might be the way to go. Then again I don't need the small peaks. I even thought about smoothing the second signal. So in my case it maybe doesn' really matter if I sample up or down. $\endgroup$ – NelsonOrange Nov 20 '17 at 8:06
18
$\begingroup$

In short:

  • Upsampling: does/should not loose information (if done wisely), then safer,
  • Downsampling: may loose information (if done unwisely), yet more computationally efficient.

So if you compare data at different rates, and in an evaluation phase when one tries to define how the comparison should be done (which features are compared, with what metric, with which externals, like precision, efficiently, robustness, etc), a very basic first approach would be to upsample both signals by an integer factor to reconcile their sampling. Here, this is quite OK, 6000 Hz could work for both.

The reasons could be, for DSP practice:

  • with integer upsampling, you are not obliged to use tricky filtering techniques, and simple linear interpolation is easy. Remember though that upsampling adds some information.
  • with signals are the same scale, you can extract features and compare them: noise, variability, slopes, derivatives, etc. You can apply the correct scale/offset correction (they don't have the same amplitude), use linear or non-linear transformations (Fourier, etc.) to check whether similarities features exist in different domains
  • at this stage, you should be able to quality and quantify the features/metrics needed to build your algorithm. And you can estimate what you can loose from both data. For instance, you can create your algorithm, and see how it is robust to signal downsampling, and how far you can go. Here, your algorithm begin to be more efficient.

Once you have spent some time on that, you are more ready to start from scratch again, and decide whether you should downsample or not, with which method, etc. to reach both safety and efficienty.

To provide alternatives, if you decide to compare data on the Fourier domain, you can easily cast two signals with different length and sampling rate to the same number of Fourier coefficients, helping a comparison in the frequency domain.

$\endgroup$
  • 14
    $\begingroup$ Downsampling will almost always lose information, even if it's not done in an unwise manner. However losing information might actually be a wise thing, so... $\endgroup$ – leftaroundabout Nov 19 '17 at 23:53
  • $\begingroup$ @leftaroundabout In what edge cases can downsampling not lose information? $\endgroup$ – Willem Nov 20 '17 at 12:07
  • 2
    $\begingroup$ @leftaroundabout I do not agree. Information losing is always worse than keeping it. The only wise thing is to know which information to throw away for a better representation of the signal, not information loss itself. $\endgroup$ – AlexTP Nov 20 '17 at 12:07
  • 5
    $\begingroup$ @Willem: As an extremely obvious case, a DC signal can be downsampled to a single sample. In general, any signal that has no components above the (new) Nyquist limit can be safely downsampled. $\endgroup$ – MSalters Nov 20 '17 at 12:45
  • 1
    $\begingroup$ It can be argued that no information is lost from a DC signal to a 1 sample signal since you can convert back to a signal identical to the original one from 1 sample. It can be argued that information is lost because if you give the 1 sample signal to a person without further information and ask "Does this represent a DC signal?" the answer will be "Don't know, not enough information with one sample". $\endgroup$ – Jose Antonio Dura Olmos Nov 20 '17 at 18:14
7
$\begingroup$

If you use a function like plot(x,y) the easiest way to display them on the same graph is to simply not resample any of them at all, but simply fill each x vector with proper values for each signal, so both appear where you want on the display.

You can also setup the plot to have two different x-axes (one for each curve) with different labels and legends if you want.

Now, about resampling. I'll use Fs for the sampling frequency.

A sampled signal cannot contain frequency components above Fs/2. It is bandlimited.

Also, a signal that only contains frequency components up to a frequency F can be accurately represented at a sampling rate of 2F.

Note that this "accurate" representation is mathematical, not visual. For a good visual representation, having 5-10 samples per period (thus no notable frequency components above Fs/10 or so) really helps the brain connect the dots. See this figure: same signal, the lower curve has lower sample rate, there is no information loss because the frequency is lower than Fs/2 but it still looks like crap.

enter image description here

It's the exact same signal though. If you oversample (reconstruct) the one on the bottom with a sinc filter you'll get the one at the top.

Decimation (downsampling) will fold back all frequency components higher than the new Fs/2 into the signal. This is why we usually put a steep lowpass filter before the decimator. For example, to downsample from Fs=2000 Hz down to Fs=30 Hz, first we would apply a high order lowpass with a cutoff a bit below 15 hz and only then decimate.

However this filter will introduce transient response issues, it will have phase lag at certain frequencies, and it may change the visual aspect of your signal, which you don't want to do if the idea is to visually compare them. The rule above applies, don't downsample too much, always keep Fs as 5-10x the highest frequency of interest if you want the signal shape to mean something. This is why a 200MHz scope needs to sample at 1-2 Gsps.

My question is: Is it wiser to downsample the second curve or to upsample the first one?

As said above, the wisest is to not mess with the data at all and simply present them each with their own x-axis on the same graph.

Sampling rate conversion would be required in some cases. For example to reduce the number of points, reduce memory use, make it faster... or to make both signals use the same "x" coordinates to perform calculations on them.

In this case you can also use an intermediate Fs, downsample the signal with high Fs and upsample the one with low Fs. Or just downsample the one with high Fs.

Mind the Nyquist criteria, and don't pick a too low sample rate or you will lose waveform shape fidelity on the high Fs signal, you'll get phase shifts because of the lowpass filter, etc. Or if you know the high frequency content is negligible, you can make an informed choice. I

If you use linear interpolation to make the "x" coordinates match, remember it also needs a quite high Fs. Interpolation would work on the top signal in the plot above, it would not work on the one on the bottom. Same if you are interested in min, max and such.

And... note that oversampling/upsampling will also mess with the transient response, at least visually. For example if you oversample a step, you will get lots of ringing due to the sinc filter impulse response. This is because you get a bandlimited signal, and a nice step with square corners actually has infinite bandwidth.

I'll take a square wave as an example. Think about the original sampled signal: 0 0 0 1 1 1 0 0 0 1 1 1... Your brain sees a square wave.

But the reality is that you should picture each sample as a dot, and there is nothing between the dots. It's the whole point of sampling. There is nothing between the samples. So when this square wave has been oversampled using a sinc interpolation... it looks funny.

enter image description here

This is simply the visual representation of a bandlimited square wave. The wiggles kinda exist... or maybe not. There is no way to know if they were there in the original signal or not. In this case the solution would have been to acquire the original square wave with a higher sampling rate to get better resolution on the edge, ideally you want several samples on your edge so it no longer looks lits a step of infinte bandwidth. Then when oversampling such a signal, the result will not have visual artifacts.

Anyway. As you can see... just mess with the x axes. It's a lot simpler.

$\endgroup$
4
$\begingroup$

Downsampling loses information. Upsampling is lossless when the factor is an integer (taken you also remember the factor), but some information is lost when the factor is not an integer. Upsampling could theoretically lose more information than downsampling, for very specific resampling factors.

Which one you should use? It depends on the level of certainty you need.

If you don't need mathematical certainty and just want a heuristic, downsampling is faster and upsampling is more accurate.

If you need to put bounds on the accuracy of your computation: it is possible but I can't help you with that.

$\endgroup$
  • $\begingroup$ +1, because you managed to squeeze a few important pieces of information in a very concise answer. $\endgroup$ – dsp_user Nov 21 '17 at 8:28
3
$\begingroup$

It depends on what you mean by "compare" and "wiser". The wise thing, which isn't hard in Matlab, is to do it both ways and decide for yourself.

Actually, if you amended your question with results from both approaches, I would upvote your question and more people would find it interesting, and most likely help in figuring out "compare"

$\endgroup$
1
$\begingroup$

I would like to point out a problem that takes place when doing upsampling that could be crucial in this operation. When a signal is upsampled and the data endpoints are far from zero values then the edge effect takes place. In practical experiences, this undesired effect should be eliminated. I share with this community a short essay with images and code I wrote about it that could help to understand.

https://medium.com/@davidpinyol91/edge-effects-when-resampling-a-signal-on-matlab-how-to-solve-it-cc6458ab1306

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.