Files
lec-sdr/cdma.Rmd
2026-03-23 21:22:50 +01:00

138 lines
3.7 KiB
Plaintext

---
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){
r <- as.integer(intToBits(utf8ToInt(s)))
r <- r[1:7]
return(r)
}
lines_bin <- list()
for (line in lines){
line_bin <- list()
for (char in strsplit(line, "")[[1]]){
s <- strToBin(char)
line_bin <- append(line_bin, s)
}
lines_bin <- append(lines_bin, list(line_bin))
}
lines_bin <- do.call(rbind,lines_bin)
```
```{r input-demo, echo=FALSE}
print(lines)
print(lines_bin)
```
```{r, results='asis', echo=FALSE}
cat("\\newpage")
```
## 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)
```
We can now do a simple check if all the values we decoded are the same:
```{r decode-demo}
decoded_m == lines_bin
```
```{r, results='asis', echo=FALSE}
cat("\\newpage")
```
Then we apply our text encoding on each line separately, respecting the LSB/MSB order.
```{r text-decode}
chunk <- function(x, chunk_size) {
chunks <- split(x, cut(seq_along(x), breaks =
seq(0, length(x), by = chunk_size), include.lowest = TRUE))
lapply(chunks, as.list)
}
final <- c('','','','')
for (i in 1:nrow(decoded_m)){
line <- decoded_m[i,]
symbols <- chunk(line, 7)
for (j in 1:length(symbols)){
binary_str <- paste(rev(symbols[[j]]), collapse = "")
# Convert the binary string to a decimal value
decimal <- strtoi(binary_str, base = 2)
# Convert the decimal value to an ASCII character
char <- intToUtf8(decimal)
final[i] <- paste0(final[i], char)
}
}
```
```{r text-decode-demo, echo=FALSE}
print(final)
```