DFT kłopot z zastosowaniem

0

Witam, staram się do napisanego kodu generującego sygnał sinusoidalny dołączyć dyskretną transformatę Fouriera tego sygnału oraz wykreślić ją w ramce poniżej sinusa. Niestety nie posiadam wystarczającej wiedzy, aby tego dokonać. Prosiłbym o wszelkie sugestie. Implementacje DFT pobrałem z tej strony http://www.nayuki.io/page/how-to-implement-the-discrete-fourier-transform. Wydaje się najmniej skomplikowana w mojej ocenie z dostępnych w sieci. Kod nie generuje żadnych błędów. Niestety nie wiem jak zabrać się do "przepuszczenia" tablicy sinusa przez kod DFT. Jak również nie wiem czy sama jego lokalizacja jest właściwa. Kod stworzony w VS 2015.

 #pragma once
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <vector>
namespace Project1 {

#include <math.h>
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label13;
	protected:
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::TextBox^  czestotliwosc;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label1;




	private: System::Windows::Forms::Label^  label18;
	private: System::Windows::Forms::TextBox^  okres_amp;

	private: System::Windows::Forms::Label^  label15;
	private: System::Windows::Forms::Label^  label16;
	private: System::Windows::Forms::Label^  label14;
	private: System::Windows::Forms::Label^  label3;
	protected:
	private:
		/// <summary>
		
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

		void InitializeComponent(void)
		{
			this->label13 = (gcnew System::Windows::Forms::Label());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->czestotliwosc = (gcnew System::Windows::Forms::TextBox());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label18 = (gcnew System::Windows::Forms::Label());
			this->okres_amp = (gcnew System::Windows::Forms::TextBox());
			this->label15 = (gcnew System::Windows::Forms::Label());
			this->label16 = (gcnew System::Windows::Forms::Label());
			this->label14 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label13
			// 
			this->label13->Location = System::Drawing::Point(0, 0);
			this->label13->Margin = System::Windows::Forms::Padding(2, 0, 2, 0);
			this->label13->Name = L"label13";
			this->label13->Size = System::Drawing::Size(67, 15);
			this->label13->TabIndex = 54;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(312, 258);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(85, 40);
			this->button1->TabIndex = 40;
			this->button1->Text = L"SYMULACJA";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// czestotliwosc
			// 
			this->czestotliwosc->Location = System::Drawing::Point(14, 151);
			this->czestotliwosc->Name = L"czestotliwosc";
			this->czestotliwosc->Size = System::Drawing::Size(100, 20);
			this->czestotliwosc->TabIndex = 31;
			this->czestotliwosc->Text = L"50";
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(11, 134);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(37, 13);
			this->label5->TabIndex = 30;
			this->label5->Text = L"fs [Hz]";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(14, 59);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 20);
			this->textBox1->TabIndex = 25;
			this->textBox1->Text = L"1";
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(11, 42);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(53, 13);
			this->label2->TabIndex = 24;
			this->label2->Text = L"Amplituda";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(238)));
			this->label1->Location = System::Drawing::Point(11, 11);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(95, 20);
			this->label1->TabIndex = 23;
			this->label1->Text = L"Symulacja:";
			this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleLeft;
			// 
			// label18
			// 
			this->label18->AutoSize = true;
			this->label18->Location = System::Drawing::Point(11, 88);
			this->label18->Name = L"label18";
			this->label18->Size = System::Drawing::Size(118, 13);
			this->label18->TabIndex = 26;
			this->label18->Text = L"ilość okresów amplitudy";
			// 
			// okres_amp
			// 
			this->okres_amp->Location = System::Drawing::Point(14, 105);
			this->okres_amp->Name = L"okres_amp";
			this->okres_amp->Size = System::Drawing::Size(100, 20);
			this->okres_amp->TabIndex = 49;
			this->okres_amp->Text = L"2";
			// 
			// label15
			// 
			this->label15->AutoSize = true;
			this->label15->Location = System::Drawing::Point(568, 88);
			this->label15->Name = L"label15";
			this->label15->Size = System::Drawing::Size(0, 13);
			this->label15->TabIndex = 51;
			// 
			// label16
			// 
			this->label16->AutoSize = true;
			this->label16->Location = System::Drawing::Point(552, 88);
			this->label16->Name = L"label16";
			this->label16->Size = System::Drawing::Size(0, 13);
			this->label16->TabIndex = 52;
			// 
			// label14
			// 
			this->label14->AutoSize = true;
			this->label14->Location = System::Drawing::Point(592, 88);
			this->label14->Name = L"label14";
			this->label14->Size = System::Drawing::Size(0, 13);
			this->label14->TabIndex = 53;
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(517, 75);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(35, 13);
			this->label3->TabIndex = 55;
			this->label3->Text = L"label3";
			this->label3->Click += gcnew System::EventHandler(this, &Form1::label3_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(755, 301);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label14);
			this->Controls->Add(this->label16);
			this->Controls->Add(this->label15);
			this->Controls->Add(this->okres_amp);
			this->Controls->Add(this->label18);
			this->Controls->Add(this->label13);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->czestotliwosc);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->Name = L"Form1";
			this->Text = L"Symulacja szybkiej transfortmay fouriera";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

			
		double n1, ns, fs, sn, f, pred_z, nap_U, x, U;
		double  t, tt = 0, y_sin, roz_t;
		double pi = 3.14159265359;
		int i = 0, x0;
		double y0;
		double T = 0;	
		int pb;  
				
		f = Convert::ToDouble(czestotliwosc->Text);

		T = (1 / f) * 1000;
		f = (int)(f * 10) / 10.0; 

		T = (int)(T * 10) / 10.0; 

		label16->Text = ("T=");
		label14->Text = ("ms");
		label15->Text = Convert::ToString(T);

		Graphics^ g1 = this->CreateGraphics();
		g1->Clear(Color::White);                 // Czyszczenie obrazu
		Pen^ pioro1 = gcnew Pen(System::Drawing::Color::Black);
		Pen^ pioro2 = gcnew Pen(System::Drawing::Color::Black);
		Pen^ pioro3 = gcnew Pen(System::Drawing::Color::Red);
		pioro1->Width = 2;
		pioro2->Width = 1;
		pioro3->Width = 2;

		g1->DrawLine(pioro1, 405, 0, 750, 0);   
		g1->DrawLine(pioro1, 750, 0, 750, 160);
		g1->DrawLine(pioro1, 750, 160, 405, 160);
		g1->DrawLine(pioro1, 405, 160, 405, 0);

		g1->DrawLine(pioro2, 405, 80, 750, 80);  

		g1->DrawLine(pioro2, 405, 235, 750, 235);   

		g1->DrawLine(pioro1, 405, 165, 750, 165);   
		g1->DrawLine(pioro1, 750, 165, 750, 305);
		g1->DrawLine(pioro1, 750, 305, 405, 305);
		g1->DrawLine(pioro1, 405, 305, 405, 165);

		int okres;
		double a;
		array<int>^ sinus_tab = gcnew array<int>(345);


		okres = Convert::ToInt32(okres_amp->Text);

		t = okres / f;			

		roz_t = t / 344;     
		a = Convert::ToDouble(textBox1->Text);
		double wart_tab[345];

		while (tt < t)			
		{
			if (tt == 0)				
			{
				y_sin = a*sin(2 * pi*f*tt) * 70;
			}
			else
			{
				y_sin = a*sin(2 * pi*f*tt) * 70;

				g1->DrawLine(pioro3, x0 + 405, 80 - Convert::ToInt32(y0), i + 405, 80 - Convert::ToInt32(y_sin));

			}

			y0 = y_sin;
			x0 = i;
			wart_tab[i] = y0;
			label3 ->Text = Convert::ToString(wart_tab[344]);
			sinus_tab[i] = 80 - Convert::ToInt32(y0); 
			//computeDft(sinus_tab)
			tt = tt + roz_t;
			i = i + 1;

		}
		// obliczenie DFT oraz jego wykreślenie

		//	g1->DrawLine(pioro3, 405 + i - 1, fal0, 405 + i, fal);	

	}

			 void computeDft(const std::vector<double> &inreal, const std::vector<double> &inimag,
				 std::vector<double> &outreal, std::vector<double> &outimag) {

				 unsigned int n = inreal.size();
				 for (unsigned int k = 0; k < n; k++) { 
					 double sumreal = 0;
					 double sumimag = 0;
					 for (unsigned int t = 0; t < n; t++) { 
						 double angle = 2 * M_PI * t * k / n;
						 sumreal += inreal[t] * cos(angle) + inimag[t] * sin(angle);
						 sumimag += -inreal[t] * sin(angle) + inimag[t] * cos(angle);
					 }
					 outreal[k] = sumreal;
					 outimag[k] = sumimag;

				 }
			 }

	private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
	}
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
	}

	private: System::Void label3_Click(System::Object^  sender, System::EventArgs^  e) {
	}
};
}
0

po pierwsze to funkcja computeDft przyjmuje std::vector<double>, a ty tworzysz i wypełniasz cli::array<int>.

zupełnie inny kontener z innym typem zawartości.

1 użytkowników online, w tym zalogowanych: 0, gości: 1