From b80ac9d9319c02e4ab576c2efcb534c58f7d53d8 Mon Sep 17 00:00:00 2001 From: eneller Date: Mon, 23 Mar 2026 19:26:22 +0100 Subject: [PATCH] begin cdma decode --- cdma.Rmd | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/cdma.Rmd b/cdma.Rmd index aba56d6..18affc7 100644 --- a/cdma.Rmd +++ b/cdma.Rmd @@ -62,7 +62,7 @@ perms <- combn(x = orthogonals,m = 2, simplify = FALSE) ```{r encode} -output = c() +output <- c() for (j in 1:ncol(lines_bin)){ col <- lines_bin[,j] output_symbol <- 0 @@ -73,8 +73,43 @@ for (j in 1:ncol(lines_bin)){ 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, output_symbol) + 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) + } +} + +``` \ No newline at end of file