1.  
  2. Manipulate[
  3. (*Nasser M. Abbasi, June 20, 2014*)
  4. (*the signal is cosine, and the SNR is given as ratio of variances*)
  5. Module[{nPoints, x0, varx0, n, x, del},
  6. nPoints = 128;
  7. del = 4 Pi/(nPoints - 1);
  8. x0 = Cos[Range[0, 4*Pi, del]];
  9. varx0 = Variance[x0];
  10. SeedRandom[0];
  11. n = RandomVariate[NormalDistribution[0, 1], nPoints];
  12. x = x0 + Sqrt[varx0/snr]*n;
  13. Show[ListLinePlot[x0, PlotStyle -> Blue], ListLinePlot[x, PlotStyle -> Red], PlotRange -> {{0, Round[xmax/del]}, {-ymax, ymax}},
  14. ImagePadding -> {{40, 15}, {40, 40}}, Frame -> True, ImageSize -> 400,
  15. FrameLabel -> {{"y(t)", None}, {"time", "Adding noise to signal"}}, Axes -> None]
  16. ],
  17. Grid[{
  18. {"SNR",
  19. Manipulator[Dynamic[snr, {snr = #} &], {1, 999, 1}, ImageSize -> Tiny],
  20. Dynamic@Row[{NumberForm[snr, 3], " (", NumberForm[10. Log[10, snr], 4], " db)"}]
  21. },
  22. {"x max",
  23. Manipulator[Dynamic[xmax, {xmax = #} &], {.1, 4.*Pi, .1}, ImageSize -> Tiny],
  24. Dynamic[xmax]
  25. },
  26. {"y max",
  27. Manipulator[Dynamic[ymax, {ymax = #} &], {.1, 1.4, .1}, ImageSize -> Tiny],
  28. Dynamic[ymax]
  29. }
  30. }, Alignment -> Left
  31. ],
  32. {{snr, 50}, None},
  33. {{xmax, 4.*Pi}, None},
  34. {{ymax, 1.4}, None}
  35. ]