Low flow smoothing


New Member
Hi there!

I was wondering whether anyone knew a solution within R for low signal smoothing of data. I have attached a screenshot showing what I require.

I know that MATLAB offers the curve fitting tool which helps with this, but I was wondering what kind of R function would be required to interpolate between points in the below example - a kind of moving average but dynamic depending on the proximity of values.

Any help would be greatly appreciated.





Ambassador to the humans
I see what you're going for there. I don't think there is anything built in. There might be a package that does that for you but I don't know of one off the top of my head. It wouldn't be too difficult to program a function to do that directly though.

flowsmooth <- function(x){
  # Assumes x is a vector of 0s and 1s
  # If a 1 isn't the last value then it will
  # fill the end with NAs

  if(!any(x == 1)){
    return(rep(NA, length(x)))

  # Get the indices for the 1s
  w <- which(x == 1)
  # The index for the first 1 tells us how many spots to fill
  # the diff between the indices tells us the subsequent counts
  vals <- c(w[1], diff(w))

  tmp <- rep(1/vals, vals)
  if(length(tmp) == length(x)){
    answer <- tmp
    answer <- c(tmp, rep(NA, length(x) - length(tmp)))

x <- c(1, 1, 1, 0, 1)

x <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1)

x <- c(0, 0, 0)

x <- c(0, 1, 0, 0)


Less is more. Stay pure. Stay poor.
Ahh, I guess I saw there was binary data but figured their real data was ordinal or didn't register it.

Of note, some people have to use low flow smoothing like 12 times to have success. Not me though.


New Member
Hi there,

Thanks for your answers. Essentially, I want to do this with real-world data, rather than binary data (it's derived from outflow, where the tips are pulses and are at 100ml resolution and we wanted to screen out the low flow (like the diagram attached).

The raw pulse data is difficult to see the trends (see grey lines)



Ambassador to the humans
Ok. Well I have no idea what your data looks like or what you want the output to look like then. Maybe provide some sample data and expected output?


New Member
Sure - here is a sample of some of the outflow data: https://easyupload.io/b1vk6l.

Ideally, I'd like it to capture the peaks at the values above 100 ml, but I'd like the smoothing to allow trends to be distinguished at the lowest flows.

Whether there is a way of filtering or smoothing out flow flows, whilst preserving the peaks of the data?

Kind regards