---
title: "CDMA"
subtitle: "Software Defined Radio"
output: pdf_document
date: "`r Sys.Date()`"
---
# Assignment
In the R environment for statistical computing, develop
software support for generating a CDMA composite
signal. Provide four parallel communication channels
with corresponding characteristic code sequences of 5
bits length. Define software support for accepting
ASCII characters (via a terminal or reading from a data
file), converting ASCII characters into binary data,
coding with the respective code sequence of the given
communication channel, and decoding and deciding
on the receiving side.
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Input and binary interpretation
We first get input by reading a file `input.txt`, where each line (0-3) is one communication channel.
Because UTF8 maintains compatibility with ASCII, the UTF8 conversion can be used assuming that the input is only ASCII.
ASCII characters require 7 bits of information per character.
```{r input}
lines <- readLines('./input.txt',encoding = 'ASCII')
strToBin <- function(s){
  # FIXME for strings longer than 1
  r <- as.integer(intToBits(utf8ToInt(s)))
  r <- r[1:7]
  return(r)
}
lines_bin <- do.call(rbind, lapply(lines, strToBin))
```

```{r input-demo, echo=FALSE}
print(lines)
print(lines_bin[1])
print(lines_bin[2])
print(lines_bin[3])
print(lines_bin[4])

```

## Orthogonal Code
For an orthogonal code, we are looking for vectors $(a,b,...n)$ that satisfy pairwise orthogonality $a \perp b$.
```{r orth}
o1 <- c( 1, 1, 1, 1, 1)
o2 <- c(-1,-1, 1, 1, 1)
o3 <- c( 1,-1, 1,-1,-1)
o4 <- c(-1, 1, 1,-1,-1)
orthogonals <- do.call(rbind,list(o1,o2,o3,o4))
```

```{r orth-demo, include=FALSE}
perms <- combn(x = orthogonals,m = 2, simplify = FALSE)
```

## Encode


```{r encode}
output <- c()
for (j in 1:ncol(lines_bin)){
  col <- lines_bin[,j]
  output_symbol <- 0
  for (i in 1:length(col)){
    symbol <- col[i] # the symbol to be encoded
    code <- orthogonals[i,] # the code vector

    single_symbol <- (-1)^(1+symbol) * code # the resulting encoded vector
    output_symbol <- output_symbol + single_symbol # the sum of all encoded vectors
  }
  output <-append(output, list(output_symbol))
}
```

## Decode
Decoding CDMA is achieved by calculating the dot product of the received encoded symbol with each channel's characteristic code.
```{r decode}
decoded <- c()
for (symbol in output){
  decoded_col <- c()
  for(i in 1:nrow(orthogonals)){
    code <- orthogonals[i,]
    decoded_symbol <- if(code%*% symbol > 0) 1 else 0
    decoded_col <- append(decoded_col,decoded_symbol)
  }
  decoded<- append(decoded, list(decoded_col))
}
decoded_m <- do.call(cbind,decoded)
```

Then we apply our text encoding on each line separately.
```{r text-decode}
chunk <- function(x, chunk_size) {
  split(x, cut(seq_along(x), breaks = seq(0, length(x), by = chunk_size), include.lowest = TRUE))
}
for (i in 1:nrow(decoded_m)){
  line <- decoded_m[i,]
  symbols <- chunk(line, 7)
  for (symbol in symbols){
    binary_str <- paste(symbol, collapse = "")
    # Convert the binary string to a decimal value
    decimal <- strtoi(binary_str, base = 2)
    print(decimal)
    # Convert the decimal value to an ASCII character
    char <- intToUtf8(decimal)
    print(char)
  }
}

```