M File Conversion C code

Source: Internet
Author: User

Parametet.m
Clc;clear;load ('src.mat'20482048= Exp (-j* (2*pi/8192= exp (j*1*pi/);

This file lists the parameters used in the value case, Src.mat file is the original data stored in MATLAB, data: 2048*1 data, each data is scientific notation.

Load command, you can load data from a file into a project for later use (how to use it, and also rely on a modular simulation diagram).

CZT_W/A are the complex numbers in exponential form: the operations of complex numbers are defined in the complex--<complex> in the C + + language, including the exponential operations of complex numbers--pow (A, b) =a^b and exp (a) =e^a.

The form of complex numbers defined in C + +: Complex <double> var_name (Real_port,imag_port);

This is similar to the definition of a complex variable is called a real and virtual part of the function of assignment, in fact, in C + + is a definition of a class, the definition of complex is the definition of the object, to the complex assignment can only be initialized mode assignment-call constructor assignment, the second assignment is the object assignment value. (Current knowledge of ....) )

Define the form of a plural class (similar)

Class Complex

{

Public

Double Real_port;

Double Imag_port;

};

For example:

Complex <double> ABC (1.2,3.12);

So:

Abc= (1.2,3.12)

Real (ABC) = 1.2; IMAG (ABC) =3.12

Complex <double> BBB; BBB=ABC; -->bbb= (1.2,3.12)

With this understanding, so:

Parameter.h is good to write.

1#include <complex>2#include <vector>3#include <iostream>4 5 using namespacestd;6 7 DoublePI =3.14159265359;8 9 intCzt_n =2048;Ten intCzt_m =2048; OneComplex <Double> Czt_w_1 (0,-2*pi/8192); AComplex <Double> Czt_w =exp (czt_w_1); -Complex <Double> czt_a_1 (0, pi/ the); -Complex <Double> CZT_A = exp (czt_w_1);
CZT.M:
function y=CZT (u,czt_n,czt_m,czt_w,czt_a) L= Czt_m + czt_n; %l=4096g= Zeros (L,1); %G is a full 0 matrix of L row 1 columns forI=0: czt_n-1%Matrix G element Assignment g (I+1,1) = czt_a^ (-i) *czt_w^ (i^2/2) *u (i+1,1); ENDG=FFT (g); h= Zeros (L,1); forI=0: czt_m-1H (I+1,1) =czt_w^ (-i^2/2); End fori=l-czt_n+1: L-1H (I+1,1) =czt_w^ (-(l-i) ^2/2); end H=FFT (h); Q=h.*g;q=Ifft (Q); X=zeros (Czt_m,1); forI=0: czt_m-1X (i+1,1) =czt_w^ (i^2/2) *q (i+1); Endy=x;

The czt.m file defines a function--czt. (The file name is the same as the function name)

function is a keyword in MATLAB that defines functions, Y is the return value, U,czt_n,czt_m,czt_w,czt_a, represents the parameter, and the function is understood by: U,czt_n,czt_m,czt_w,czt_ A these parameters go through a series of steps to get the process of outputting y, these steps are the function body, where u represents what? , it is a matrix, the element is the data in the Src.mat file, so u is a 2048*1 matrix (the matrix can be replaced with an array in the C locale).

Command the entire: The calculation methods used are: FFT, Ifft, the matrix of the point multiplication, the complex number of the exponential operation.

L = Czt_m + czt_n; %l=4096= Zeros (L,1); %g for L row 1 columns of all 0 matrices for  i=0: czt_n-1 Matrix g element Assignment    g (i+1,1 ) = czt_a^ (-i) *czt_w^ (i^2/2) *u (i+1,1); ENDG=fft (g);

In writing this part of the code, the first thing to do is to do some preparatory work:

1, G matrix is a 4096*1 matrix, can be replaced with one-dimensional array, the element of the array is a complex character.

2, the elements of the U matrix are stored in the form of a file before this, to read into the U variable (src_real.txt only recorded the real part, Src_imag.txt only recorded the imaginary part)

3, G = Zeros (l,1) represents the variable initialization process, so the array is initialized.

4, it is noteworthy that the G here to participate in the operation below, so it can not be a function of local variables, but also to extract the FFT function, so that it can be accessed in subsequent calculations.

1 //test_9.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <vector>6#include <complex>7#include <iostream>8#include"Parameter.h"9#include"fftw3.h"Ten#include <stdlib.h> One#include <malloc.h> A  -  -Complex <Double> u[2048]; the  - /*********** read raw data to Matrix (array u) ***************/ - voidSrc_txt_u () - { +         intI=0; -         DoubleU_real=0; +         Doubleu_imag=0; A  atFILE *fp_1,*fp_2; -Fp_1=fopen ("Real_src.txt","R"); -         if(fp_1==NULL) -cout<<"Fopen_error"<<Endl; -Fp_2=fopen ("Imag_src.txt","R"); -         if(fp_2==NULL) incout<<"Fopen_error"<<Endl; -  to          for(i=0;i<2048; i++) +         { -FSCANF (Fp_1,"%LF",&u_real); theFSCANF (fp_2,"%LF",&u_imag); *Complex <Double>U_mid (u_real,u_imag); $u[i]=U_mid;Panax Notoginseng         } - fclose (fp_1); the fclose (fp_2); +}

When reading data from a file into a variable, the function used is--fscanf ().

Since complex numbers are both defined and assigned, they are done at the same time and cannot be executed separately, so this defines an intermediate variable--u_mid it to assign the final complex number to the corresponding variable-the element of the array.

1 /************** calculation g=fft (g) **************/2Complex <Double>*Fft_g ()3 {4     //Get Matrix G5Complex <Double> *G;6     intl=czt_n+czt_m;7g = (Complex <Double> *)malloc(sizeof(Complex <Double>) *L);8      for(intI=0; i<czt_n;i++)9G[i]=pow (czt_a,-i) *pow (czt_w,i*i/2)*U[i];Ten  One     //FFT calculation AFftw_complex *in_put,*Out_put; -Complex <Double> *G_mid; -G_mid = (Complex <Double> *)malloc(sizeof(Complex <Double>) *2048); the Fftw_plan p_1; -     intN =2048; -in_put= (fftw_complex*) Fftw_malloc (sizeof(Fftw_complex) *N); -out_put= (fftw_complex*) Fftw_malloc (sizeof(Fftw_complex) *N); +      for(intI=0; i < N; i++) -     { +in_put[i][0] =Real (g[i]); Ain_put[i][1] =imag (G[i]); at         //printf ("%lf+%lfj\n", in_put[i][0],in_put[i][1]); -     } -p_1=fftw_plan_dft_1d (N,in_put,out_put, Fftw_forward, fftw_estimate); - Fftw_execute (p_1); -      for(intj =0; J < n;j++) -     { inComplex <Double> Var_mid (out_put[j][0],out_put[j][1]); -g_mid[j]=Var_mid; to     } +      -      for(intj =0; J < n;j++) the     { *         //printf ("%lf+%lfj\n", Real (G_mid[j]), Imag (G_mid[j])); $     }Panax Notoginseng Fftw_destroy_plan (p_1); - Fftw_free (in_put); the Fftw_free (out_put); +     returnG_mid; A}

Note the point:

1, when defining the G array, cannot use: Complex <double> g[l] This form, because L is a variable, then the static definition of the array is not feasible. In a dynamic definition, be sure to allocate enough space, where the G array represents the 4096*1 matrix space, then malloc (sizeof (complex <double>) *l space is required. If you only define a complex number of spaces (complex <double> *g), then only the first element position can be stored, and the other elements cannot be used.

2. Fftw_complex types and Compelx <double> types of data cannot be cast or implicitly converted, but can be assigned using both real and virtual parts:

for (int i=0; i < N; i++)
{
In_put[i][0] = Real (g[i]);
IN_PUT[I][1] = Imag (G[i]);
}

for (int j = 0;j < n;j++)

{

Complex <double> Var_mid (out_put[j][0],out_put[j][1]);
G_mid[j]=var_mid;
}

  

M File Conversion C code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.