1

How could be possible to represent (plot and numerically) a sawthooth signal in R from:

y <- c(NA,NA,NA,NA,1,NA,NA,NA,1,NA,NA,NA,NA,NA,1,NA,NA,NA,NA,1,NA)

where 1 represents in y the time points when the sawtooth achieves a peak (obviously to 1). Note that the distance between peaks are unequal.

I thought about using interpolation but maybe it is unnecessary.

Thank you,

fina
  • 429
  • 4
  • 12

1 Answers1

0

You can create a sequence of falling numbers like this:

peaks <- c(0, which(!is.na(y)), length(y))

drop <- -1/max(diff(peaks))

df <- do.call(rbind, lapply(diff(peaks), function(x) {
  data.frame(x = c(0, rep(1, x)), 
             y = c(1, seq(1 + drop, by = drop, length.out = x)))
}))

df$x <- cumsum(df$x)

Which gives this result:

plot(df$x, df$y, type = "l")

Or if you want to be fancy...

library(ggplot2)

ggplot(df, aes(x, y)) +
  geom_line(col = "deepskyblue4", size = 1.5) +
  theme_bw()

Created on 2020-09-18 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • I was expecting the time points with 1 being completely straight @Allan Cameron – fina Sep 18 '20 at 18:30
  • 1
    @fina that's just the way it's plotted. If the vector represents equal time steps sampling a signal, then you would only get a straight line up if you have a zero and a one at exactly the same time. Of course it's possible to do this, but I thought you were interested in the vector being converted to a sawtooth signal. Is it just the plot you want? If so, which version - the first or second? – Allan Cameron Sep 18 '20 at 18:32
  • The second, probably using two different vectors and plotting separately (in the same plot). – fina Sep 18 '20 at 18:38
  • Not that easy eh... I'm trying too! @Allan Cameron – fina Sep 18 '20 at 19:10
  • 1
    @fina yes, a little bit fiddly. Hopefully the update does what you need. – Allan Cameron Sep 18 '20 at 19:25
  • Great @Allan Cameron ! Could be possible to have the other version (from 0)? – fina Sep 18 '20 at 19:31
  • 1
    @fina sure - just change the `seq` to `seq(1 - 1/x, 0, length.out = x)` – Allan Cameron Sep 18 '20 at 19:40
  • How I can be as good as you programming @Allan Cameron? I'm quite curious about the routine you take to create df. I am not used to this code. – fina Sep 18 '20 at 19:45
  • 1
    Thanks @fina . Hanging around the R thread here on SO is a great way to learn. I have picked up lots of great tips here. – Allan Cameron Sep 18 '20 at 19:47
  • 1
    Yes, true. But I think I should work on combine `lapply` with functions. Thank you, excellent work! – fina Sep 18 '20 at 19:50
  • `y = c(1, seq(0, 1 - 1/x, length.out = x))` I am using this (slope inverted). Do you have any idea of how calculating the angle generated by the slope side and the basis. I can open a new question if you want. @Allan Cameron I would use https://stackoverflow.com/questions/1897704/angle-between-two-vectors-in-r but I don't know how to calculate the coordinates of the slope and normalise them for all sawtooth – fina Sep 20 '20 at 08:07
  • @fina I'm not sure I understand fully - it may be best to open another question to illustrate your needs – Allan Cameron Sep 20 '20 at 08:26
  • You can find the question in https://stackoverflow.com/questions/63977620/na-sawthooth-signal-calculate-angles @Allan Cameron – fina Sep 20 '20 at 09:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221772/discussion-between-fina-and-allan-cameron). – fina Sep 20 '20 at 11:31
  • @fina I see you deleted your new question. Why was that? – Allan Cameron Sep 20 '20 at 12:00
  • because it was not well formulated @Allan Cameron and I thought it was a good exercise to solve it alone. So I did it! – fina Sep 20 '20 at 12:05