Advertisement
bvdmitri

Untitled

Dec 7th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.49 KB | None | 0 0
  1. require("akima", quietly = TRUE)
  2.  
  3. attach(mtcars)
  4. par(mfrow=c(3,2))
  5.  
  6. numberCount = 100;
  7. from = -2;
  8. to = 2;
  9. tau = 1;
  10. a = 1;
  11. m = 2;
  12.  
  13. h = 1;
  14. xreal <- from + (0:100)*(to - from)/(100)
  15. x <- from + (0:numberCount)*(to - from)/(numberCount)
  16.  
  17. discrete <- function(x, ap = a, taup = tau) ifelse(x <= taup/2, ifelse(x >= -taup/2, ap, 0), 0)
  18. triangle <- function(x, ap = a, taup = tau) ifelse(x >= -taup/2,
  19.                                                    ifelse(x <= taup/2,
  20.                                                    ifelse(x <= 0, 2 * ap / taup * (x + taup / 2),
  21.                                                    - 2 * ap / taup * (x - taup / 2)) , 0), 0);
  22. sawtooth <- function(x, ap = a, taup = tau) ifelse(x >= -taup/2,
  23.                                                    ifelse(x <= taup/2,
  24.                                                           ifelse(x <= 0, 2 * ap * x / taup + ap,
  25.                                                                  2 * ap * x / taup - ap), 0), 0);
  26.  
  27. gauss <- function(x, ap = a, taup = tau, mp = m) ap * exp(-(2*x/taup)^mp)
  28.  
  29. showGraphics <- function(func) {
  30.   valuesreal <- func(xreal);
  31.   values <- func(x)
  32.   plot(xreal, valuesreal, type = c("p"), ylab = "y", xlab = "x", main = paste("Исходный график (поточечно)"), asp = 0.8);
  33.   plot(xreal, valuesreal, type = c("l"), ylab = "y", xlab = "x", main = paste("Исходный график (линией)"), asp = 0.8);
  34.  
  35.   f <- fft(values);
  36.  
  37.   f2 = append(f, c(f[1]), after = length(f));
  38.  
  39.   realf <- Re(f);
  40.  
  41.   imaginef <- Im(f)
  42.  
  43.   barplot(Re(f2), xlab = paste("Real spectres"), main = paste("Действительная часть спектра"));
  44.   barplot(Im(f2), xlab = paste("Im spectres"), main = paste("Мнимая часть спектра"));
  45.  
  46.   invrealf <- Re(fft(f, inverse = TRUE) / length(realf))
  47.  
  48.   hx <- c();
  49.   hf <- c();
  50.  
  51.   for (i in 0:(numberCount + 1)) {
  52.     if (h == 0 || i %% h == 0) {
  53.       hx = append(hx, x[i], after = length(hx));
  54.       hf = append(hf, f[i], after = length(hf));
  55.     }
  56.   }
  57.  
  58.   hinvf <- (fft(hf, inverse = TRUE) / length(hf));
  59.  
  60.   plot(hx, hinvf, type = c("p"), ylab = "y", xlab = "x", main = paste("Восстановленный график"), asp = 0.8, col = c("white"));
  61.   #lines(spline(x = hx, y = hinvf, n = 10 * length(hx)), col = c("blue"))
  62.   plot(hx, hinvf, type = c("l"), ylab = "y", xlab = "x", main = paste("Восстановленный график (линией)"));
  63. }
  64.  
  65. showGraphics(gauss)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement