File: winfunc.R

package info (click to toggle)
r-cran-av 0.5.1+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 324 kB
  • sloc: ansic: 1,353; sh: 83; makefile: 6
file content (135 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#' Window functions
#'
#' Several common [windows function](https://en.wikipedia.org/wiki/Window_function)
#' generators. The functions return a vector of weights to use in [read_audio_fft].
#'
#' @export
#' @name window functions
#' @rdname winfunc
#' @param n size of the window (number of weights to generate)
#' @examples # Window functions
#' plot(hanning(1024), type = 'l', xlab = 'window', ylab = 'weight')
#' lines(hamming(1024), type = 'l', col = 'red')
#' lines(bartlett(1024), type = 'l', col = 'blue')
#' lines(welch(1024), type = 'l', col = 'purple')
#' lines(flattop(1024), type = 'l', col = 'darkgreen')
hanning <- function(n){
  generate_window(n, 1L)
}

#' @export
#' @rdname winfunc
hamming <- function(n){
  generate_window(n, 2L)
}

#' @export
#' @rdname winfunc
blackman <- function(n){
  generate_window(n, 3L)
}

#' @export
#' @rdname winfunc
bartlett <- function(n){
  generate_window(n, 4L)
}

#' @export
#' @rdname winfunc
welch <- function(n){
  generate_window(n, 5L)
}

#' @export
#' @rdname winfunc
flattop <- function(n){
  generate_window(n, 6L)
}

#' @export
#' @rdname winfunc
bharris <- function(n){
  generate_window(n, 7L)
}

#' @export
#' @rdname winfunc
bnuttall <- function(n){
  generate_window(n, 8L)
}

#' @export
#' @rdname winfunc
sine <- function(n){
  generate_window(n, 9L)
}

#' @export
#' @rdname winfunc
nuttall <- function(n){
  generate_window(n, 10L)
}

#' @export
#' @rdname winfunc
bhann <- function(n){
  generate_window(n, 11L)
}

#' @export
#' @rdname winfunc
lanczos <- function(n){
  generate_window(n, 12L)
}

#' @export
#' @rdname winfunc
gauss <- function(n){
  generate_window(n, 13L)
}

#' @export
#' @rdname winfunc
tukey <- function(n){
  generate_window(n, 14L)
}

#' @export
#' @rdname winfunc
dolph <- function(n){
  generate_window(n, 15L)
}

#' @export
#' @rdname winfunc
cauchy <- function(n){
  generate_window(n, 16L)
}

#' @export
#' @rdname winfunc
parzen <- function(n){
  generate_window(n, 17L)
}

#' @export
#' @rdname winfunc
bohman <- function(n){
  generate_window(n, 19L)
}

#' @useDynLib av R_generate_window
generate_window <- function(n, type){
  n <- as.integer(n)
  type <- as.integer(type)
  assert_range(n)
  assert_range(type, max = 19)
  .Call(R_generate_window, n, type)
}

assert_range <- function(x, min = 0, max = Inf){
  stopifnot(length(x) == 1)
  stopifnot(x >= min)
  stopifnot(x <= max)
}