PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Wavelets/Diplom/Forms/WaveletsDesigner.cs

#
C# | 233 lines | 217 code | 16 blank | 0 comment | 47 complexity | e9b9de42839e9d29604707ba2286065a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. namespace Diplom.Forms
  11. {
  12. public partial class WaveletsDesigner : Form
  13. {
  14. NumericArrays.Numeric1DArray array1D = null;
  15. string waveletType = string.Empty;
  16. public WaveletsDesigner()
  17. {
  18. InitializeComponent();
  19. }
  20. private void refreshButton_Click(object sender, EventArgs e)
  21. {
  22. int length;
  23. double a;
  24. double b;
  25. try
  26. {
  27. length = (int)numericUpDown1.Value;
  28. }
  29. catch (Exception ex)
  30. {
  31. textBox1.Text = ex.ToString();
  32. return;
  33. }
  34. try
  35. {
  36. string[] strs = intervalTextBox.Text.Split(new string[4] { " ", ",", ":", ";" }, StringSplitOptions.RemoveEmptyEntries);
  37. if (strs.Length != 2) throw new Exception("Incorrect Interval");
  38. a = Convert.ToDouble(strs[0]);
  39. b = Convert.ToDouble(strs[1]);
  40. }
  41. catch (Exception ex)
  42. {
  43. textBox1.Text = ex.ToString();
  44. return;
  45. }
  46. if (string.IsNullOrEmpty(comboBox1.Text))
  47. {
  48. textBox1.Text = "Please select Wavelet";
  49. return;
  50. }
  51. switch (comboBox1.Text)
  52. {
  53. case "MHAT":
  54. {
  55. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  56. double h = (b - a) / length;
  57. int i=0;
  58. for (double x = a; x <= b; x += h)
  59. {
  60. if (i == length) break;
  61. double res = (1 - x * x);
  62. res *= Math.Exp(-x * x * 0.5);
  63. array1D.Insert(i, res);
  64. i++;
  65. }
  66. array1D.InitCharacters();
  67. } break;
  68. case "Real part Morlet":
  69. {
  70. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  71. double h = (b - a) / length;
  72. int i = 0;
  73. for (double x = a; x <= b; x += h)
  74. {
  75. if (i == length) break;
  76. double res = Math.Exp(-x * x / 2.0) * Math.Cos(2 * Math.PI * x);
  77. array1D.Insert(i, res);
  78. i++;
  79. }
  80. array1D.InitCharacters();
  81. } break;
  82. case "WAVE":
  83. {
  84. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  85. double h = (b - a) / length;
  86. int i = 0;
  87. for (double x = a; x <= b; x += h)
  88. {
  89. if (i == length) break;
  90. double res = -x * Math.Exp(-x * x * 0.5);
  91. array1D.Insert(i, res);
  92. i++;
  93. }
  94. array1D.InitCharacters();
  95. } break;
  96. case "DOG":
  97. {
  98. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  99. double h = (b - a) / length;
  100. int i = 0;
  101. for (double x = a; x <= b; x += h)
  102. {
  103. if (i == length) break;
  104. double res = Math.Exp(-x * x * 0.5) - 0.5 * Math.Exp(-x * x / 8.0);
  105. array1D.Insert(i, res);
  106. i++;
  107. }
  108. array1D.InitCharacters();
  109. } break;
  110. case "HAAR":
  111. {
  112. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  113. double h = (b - a) / length;
  114. int i = 0;
  115. for (double x = a; x <= b; x += h)
  116. {
  117. if (i == length) break;
  118. double res;
  119. if (x < 0) res = 0.0;
  120. else if (x < 0.5) res = 1.0;
  121. else res = -1.0;
  122. array1D.Insert(i, res);
  123. i++;
  124. }
  125. array1D.InitCharacters();
  126. } break;
  127. case "LP-LittleWood_Paley":
  128. {
  129. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  130. double h = (b - a) / length;
  131. int i = 0;
  132. for (double x = a; x <= b; x += h)
  133. {
  134. if (i == length) break;
  135. double res;
  136. if (Math.Abs(x) < 1e-6) res = 0.0;
  137. else res = (Math.Sin(2*Math.PI*x)-Math.Sin(Math.PI*x))/(Math.PI*x);
  138. array1D.Insert(i, res);
  139. i++;
  140. }
  141. array1D.InitCharacters();
  142. } break;
  143. case "FHAT":
  144. {
  145. array1D = new Diplom.NumericArrays.Numeric1DArray(length);
  146. double h = (b - a) / length;
  147. int i = 0;
  148. for (double x = a; x <= b; x += h)
  149. {
  150. if (i == length) break;
  151. double res;
  152. if (x < -1.0) res = 0.0;
  153. else if (3 * x < -1.0) res = -0.5;
  154. else if (3 * x < 1.0) res = 1.0;
  155. else if (x < 1.0) res = -0.5;
  156. else res = 0.0;
  157. array1D.Insert(i, res);
  158. i++;
  159. }
  160. array1D.InitCharacters();
  161. } break;
  162. }
  163. waveletType = comboBox1.Text;
  164. this.RePaint();
  165. }
  166. private void WaveletsDesigner_Load(object sender, EventArgs e)
  167. {
  168. comboBox1.SelectedIndex = 0;
  169. }
  170. private void RePaint()
  171. {
  172. if (array1D != null)
  173. {
  174. Image img = new Bitmap(waveletPicture.Width, waveletPicture.Height);
  175. System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(img);
  176. graphics.Clear(Color.White);
  177. Graphics.D2D.DrawStyle drawStyle = new Diplom.Graphics.D2D.DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);
  178. Graphics.D2D.D2DVisualizer.DrawGraphic(ref graphics, ref array1D,
  179. new RectangleF(
  180. new PointF((float)50, (float)30),
  181. new SizeF((float)waveletPicture.Width - 100, (float)waveletPicture.Height - 60)),
  182. drawStyle);
  183. Graphics.D2D.D2DVisualizer.DrawSignature(ref graphics, ref array1D,
  184. new RectangleF(
  185. new PointF((float)50, (float)30),
  186. new SizeF((float)waveletPicture.Width - 100, (float)waveletPicture.Height - 60)),
  187. drawStyle, new Size(25, 10));
  188. graphics.DrawString(waveletType, new Font("Times New Roman", 7), Brushes.Red,
  189. new PointF(10, 10));
  190. waveletPicture.Image = img;
  191. }
  192. }
  193. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  194. {
  195. refreshButton_Click(sender, e);
  196. }
  197. private void saveButton_Click(object sender, EventArgs e)
  198. {
  199. if (array1D != null && waveletPicture.Image != null)
  200. {
  201. SaveFileDialog saveFileDialog = new SaveFileDialog();
  202. saveFileDialog.AddExtension = true;
  203. saveFileDialog.CheckPathExists = true;
  204. saveFileDialog.Filter = "Jpeg files|*.jpg";
  205. saveFileDialog.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
  206. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  207. {
  208. waveletPicture.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
  209. }
  210. }
  211. }
  212. private void WaveletsDesigner_ResizeEnd(object sender, EventArgs e)
  213. {
  214. this.RePaint();
  215. }
  216. }
  217. }