pulstran {gsignal} | R Documentation |
Pulse train
Description
Generate a train of pulses based on samples of a continuous function.
Usage
pulstran(
t,
d,
func,
fs = 1,
method = c("linear", "nearest", "cubic", "spline"),
...
)
Arguments
t |
Time values at which |
d |
Offset removed from the values of the array |
func |
Continuous function used to generate a pulse train based on its
samples, specified as 'rectpuls', 'gauspuls', 'tripuls', or a function
handle. If you use |
fs |
Sample rate in Hz, specified as a real scalar. |
method |
Interpolation method, specified as one of the following options:
Interpolation is performed by the function |
... |
Further arguments passed to |
Details
Generate the signal y <- sum(func(t + d, ...))
for each d
. If
d
is a matrix of two columns, the first column is the delay d
and the second column is the amplitude a
, and y <- sum(a *
func(t + d))
for each d, a
. Clearly, func
must be a function
which accepts a vector of times. Any extra arguments needed for the function
must be tagged on the end.
If instead of a function name you supply a pulse shape sampled at frequency
fs
(default 1 Hz), an interpolated version of the pulse is added at
each delay d
. The interpolation stays within the the time range of the
delayed pulse. The interpolation method defaults to linear, but it can be any
interpolation method accepted by the function interp1
Value
Pulse train generated by the function, returned as a vector.
Author(s)
Sylvain Pelissier, sylvain.pelissier@gmail.com.
Conversion to R by Geert van Boxtel G.J.M.vanBoxtel@gmail.com.
Examples
## periodic rectangular pulse
t <- seq(0, 60, 1/1e3)
d <- cbind(seq(0, 60, 2), sin(2 * pi * 0.05 * seq(0, 60, 2)))
y <- pulstran(t, d, 'rectpuls')
plot(t, y, type = "l", xlab = "Time (s)", ylab = "Waveform",
main = "Periodic rectangular pulse")
## assymetric sawtooth waveform
fs <- 1e3
t <- seq(0, 1, 1/fs)
d <- seq(0, 1, 1/3)
x <- tripuls(t, 0.2, -1)
y <- pulstran(t, d, x, fs)
plot(t, y, type = "l", xlab = "Time (s)", ylab = "Waveform",
main = "Asymmetric sawtooth waveform")
## Periodic Gaussian waveform
fs <- 1e7
tc <- 0.00025
t <- seq(-tc, tc, 1/fs)
x <- gauspuls(t, 10e3, 0.5)
plot(t, x, type="l", xlab = "Time (s)", ylab = "Waveform",
main = "Gaussian pulse")
ts <- seq(0, 0.025, 1/50e3)
d <- cbind(seq(0, 0.025, 1/1e3), sin(2 * pi * 0.1 * (0:25)))
y <- pulstran(ts, d, x, fs)
plot(ts, y, type = "l", xlab = "Time (s)", ylab = "Waveform",
main = "Gaussian pulse train")
# Custom pulse trains
fnx <- function(x, fn) sin(2 * pi * fn * x) * exp(-fn * abs(x))
ffs <- 1000
tp <- seq(0, 1, 1/ffs)
pp <- fnx(tp, 30)
plot(tp, pp, type = "l",xlab = 'Time (s)', ylab = 'Waveform',
main = "Custom pulse")
fs <- 2e3
t <- seq(0, 1.2, 1/fs)
d <- seq(0, 1, 1/3)
dd <- cbind(d, 4^-d)
z <- pulstran(t, dd, pp, ffs)
plot(t, z, type = "l", xlab = "Time (s)", ylab = "Waveform",
main = "Custom pulse train")