# transforms an integer value n to a at least min.length bit binary vector binVector <- function(n, min.length) { result <- integer() i <- 0 while (n != 0) { result <- c(n %% 2, result) n = n %/% 2 i <- i + 1 } zeros <- rep(0, times = min.length) result <- c(zeros, result) return(tail(result,min.length)) } # returns an ordering of the state numbers for a nice state diagram ordering <- function(num.states, next.state) { #next.state <- matrix(rep(0:(num.states-1), each = 2), ncol = 2) result <- integer(1) n <- 0 for (i in 2:num.states) { n0 <- next.state[n+1,1] n1 <- next.state[n+1,2] if (n0 %in% result) { n <- n1 } else if (n1 %in% result) { n <- n0 } else { n <- if (n0 > n1) n0 else n1 } result <- c(result, n) } return(result) } decToBin <- function(x) { if (all(x<2)) x else paste(decToBin(x%/%2), x%%2, sep="") } octalToDec <- function(x) { res <- numeric() for(j in 1:length(x)) { dec <- 0 i <- 0 while (x[j] > 0) { dec <- dec + (x[j] %% 10) * (8^i) i <- i + 1 x[j] <- x[j] %/% 10 } res[j] <- dec } return(res) }
conv.encoder <- params$conv.encoder M <- conv.encoder$M N <- conv.encoder$N generators <- conv.encoder$generators output <- conv.encoder$output next.state <- conv.encoder$next.state prev.state <- conv.encoder$prev.state rsc.flag <- conv.encoder$rsc decoded <- params$decoded trellis <- params$trellis survivor.states <- params$survivor.states num.states <- 2^M if (num.states > 4) { yfactor = 0.75 } else { yfactor = 1.25 } if (ncol(trellis) > 10) { xfactor = max(0.73, exp((10-ncol(trellis))/13)) } else { xfactor = 1 } tikz.trellis <- "" ## TRELLIS DIAGRAM # trellis nodes for (t in 1:ncol(trellis)) { for (s in 1:num.states) { if (is.na(trellis[s,t])) { nodetype <- "freestate" value <- "" } else { nodetype <- "state" value <- round(trellis[s,t],1) } definition <- paste0("\\node[",nodetype,"]") nodename <- paste0("(node",s-1,t-1,")") coordinate <- paste0("at (",xfactor*(t-1),",",yfactor*(-s+1),")") label <- paste0("{",value,"}") single.node <- paste(definition,nodename,coordinate,label,";") tikz.trellis <- paste(tikz.trellis, single.node) } } # state labels for (s in 0:(num.states-1)) { value <- paste(binVector(s,M),collapse = "") label <- paste0("\\node (statelabel",s,") [left of=node",s,"0, xshift=4mm] {",value,"};") tikz.trellis <- paste(tikz.trellis, label) } # decoded message under trellis diagram for (t in 0:(length(decoded)-1)) { max.state <- num.states - 1 visibility <- paste0("\\visible<",ncol(trellis)+1,">") position <- paste0("($(node",max.state,t,")!0.5!(node",max.state,t+1,")+(0,-.5)$)") decoded.bit <- paste0(visibility,"{\\node [orange, font=\\small] at",position," {",decoded[t+1],"};}") tikz.trellis <- paste(tikz.trellis, decoded.bit) } # output label visibility <- paste0("\\visible<",ncol(trellis)+1,">") position <- paste0("below=.51 of statelabel",num.states - 1,".west, anchor=west") output.label <- paste0(visibility,"{\\node [orange, font=\\small,",position,"] (outlabel) {Output :};}") tikz.trellis <- paste(tikz.trellis, output.label) # bit legend legend.bit.0 <- "\\draw [->] ($(outlabel.west)+(0,-1)$) -- ++(1,0) node (bit0) [right] {Bit 0};" legend.bit.1 <- "\\draw [->, dashed] ($(bit0.east)+(.5,0)$) -- ++(1,0) node (bit1) [right] {Bit 1};" tikz.trellis <- paste(tikz.trellis, legend.bit.0, legend.bit.1) # time labels for (t in 0:(ncol(trellis)-1)) { label <- paste0("\\node (timelabel",t,") [above of=node0",t,", yshift=-4mm] {",t,"};") tikz.trellis <- paste(tikz.trellis, label) } # node connections x <- 0 for (t in 1:(ncol(trellis)-1)) { x.next <- next.state[x+1,decoded[t]+1] for (s in 0:(num.states-1)) { if (!is.na(trellis[s+1,t])) { this.node <- paste0("(node",s,t-1,")") if (!is.na(trellis[next.state[s+1,1]+1,t+1])) { next.node.0 <- paste0("(node",next.state[s+1,1],t,")") label.value.0 <- paste(binVector(output[s+1,1],N),collapse = "") label.0 <- paste0("node [sloped, above, very near start] {",label.value.0,"}") edge.0 <- paste("\\draw[->]",this.node,"--",next.node.0,label.0,";") if (s == x && x.next == next.state[s+1,1]) { # path of the decoded message will be colored color.edge <- paste("\\draw[->, orange]",this.node,"--",next.node.0,label.0,";") edge.0 <- paste0("\\alt<",ncol(trellis)+2-t,"-> {",color.edge,"}{",edge.0,"};") } else if (survivor.states[next.state[s+1,1]+1,t+1] != s) { # remove non-survivor paths # edge.0 <- paste("\\alt<-",ncol(trellis),"> {",edge.0,"}{};") edge.0 <- paste("\\alt<1> {",edge.0,"}{};") } else { # survivor path, but not THE correct path # gray when correct path is colored orange color.edge <- paste("\\draw[->, lightgray]",this.node,"--",next.node.0,label.0,";") edge.0 <- paste0("\\alt<",ncol(trellis)+2-t,"-> {",color.edge,"}{",edge.0,"};") } tikz.trellis <- paste(tikz.trellis, edge.0) } if (!is.na(trellis[next.state[s+1,2]+1,t+1])) { next.node.1 <- paste0("(node",next.state[s+1,2],t,")") label.value.1 <- paste(binVector(output[s+1,2],N),collapse = "") label.1 <- paste0("node [sloped, above, very near start] {",label.value.1,"}") edge.1 <- paste("\\draw[->, dashed]",this.node,"--",next.node.1,label.1,";") if (s == x && x.next == next.state[s+1,2]) { # path of the decoded message will be colored color.edge <- paste("\\draw[->, dashed, orange]",this.node,"--",next.node.1,label.1,";") edge.1 <- paste0("\\alt<",ncol(trellis)+2-t,"-> {",color.edge,"}{",edge.1,"};") } else if (survivor.states[next.state[s+1,2]+1,t+1] != s) { # remove non-survivor paths at the end # edge.1 <- paste("\\alt<-",ncol(trellis),"> {",edge.1,"}{};") edge.1 <- paste("\\alt<1> {",edge.1,"}{};") } else { # survivor path, but not THE correct path # gray when correct path is colored orange color.edge <- paste("\\draw[->, dashed, lightgray]",this.node,"--",next.node.1,label.1,";") edge.1 <- paste0("\\alt<",ncol(trellis)+2-t,"-> {",color.edge,"}{",edge.1,"};") } tikz.trellis <- paste(tikz.trellis, edge.1) } } } x <- x.next }
# Kodierer Informationen if (rsc.flag == TRUE) { art.coder <- "Rekursiver" } else { art.coder <- "Nicht-Rekursiver" } if (rsc.flag == FALSE) { generator.matrix <- paste0(decToBin(octalToDec(generators)), " \\\\ ", collapse = "") generator.print <- paste0("(", paste(generators, collapse="_8,"), "_8) = \\begin{pmatrix}", generator.matrix, "\\end{pmatrix}") } else { generator.print <- paste0("\\left(1,", paste0("\\frac{", head(generators, length(generators) - 1), "_8}{", tail(generators, 1), "_8}", collapse = ","), "\\right)") }
r art.coder
Kodiererr N
$$r M
$$r generator.print
$$r N
}$$\begin{center}
next.table <- next.state colnames(next.table) <- c("Bit 0", "Bit 1") row.counter <- rep(0:(dim(next.table)[1]-1)) rownames(next.table) <- paste("Zustand ", row.counter) knitr::kable(next.table, format="latex", align="c")
\end{center}
\begin{center}
output.table <- output output.table <- matrix(decToBin(as.vector(output.table)), ncol = ncol(output.table)) colnames(output.table) <- c("Bit 0", "Bit 1") row.counter <- rep(0:(dim(next.table)[1]-1)) rownames(output.table) <- paste("Zustand ", row.counter) knitr::kable(output.table, format="latex", align="c")
\end{center}
r round(params$code, digits = 1)
)
\vspace{10mm}
\begin{equation}
\text{Kode(signal)} =
\begin{cases}
0 & \quad \text{wenn signal } \geq 0\
1 & \quad \text{sonst}\
\end{cases}
\end{equation}
\vspace{10mm}r ifelse(params$code >= 0, 0, 1)
)r if(params$soft.flag) "(soft decision)" else "(hard decision)"
Input : (r ifelse(params$code >= 0, 0, 1)
)
\vfill
\begin{tikzpicture}[>=stealth, font=\tiny]
\tikzstyle{state} = [draw, circle, teal, thick, text=black, minimum size=4mm, inner sep=0mm]
\tikzstyle{freestate} = [circle, teal, fill=teal, minimum size=1mm, inner sep=0mm]
r tikz.trellis
\end{tikzpicture}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.