C#による 1次元離散ウェーブレット変換

C#によるサンプルソースコード : 使用関数名:c09ca

Keyword: 1次元, 離散, ウェーブレット変換

概要

本サンプルは1次元離散ウェーブレット変換を行うC#によるサンプルプログラムです。 本サンプルは以下に示される8個の要素をもつ配列についてDaubechies ウェーブレットを用いて1次元離散ウェーブレット変換を行い、近似係数、詳細係数とウェーブレットの再構成を出力します。

1次元離散ウェーブレット変換のデータ 

※本サンプルはnAG Library for .NETに含まれる関数 c09ca() のExampleコードです。本サンプル及び関数の詳細情報は c09ca のマニュアルページをご参照ください。
ご相談やお問い合わせはこちらまで

入力データ

(本関数の詳細はc09ca のマニュアルページを参照)

このデータをダウンロード
c09ca Example Program Data
8                  : N
'DB4'  'Z'      : WAVNAM, MODE
1.0
3.0
5.0
7.0
6.0
4.0
5.0
2.0                : X(1:N)

  • 1行目はタイトル行で読み飛ばされます。
  • 2行目に入力データである配列の要素の数(n)を指定しています。
  • 3行目にどのウェーブレット法を使用するかを示すパラメータ(wavnam)とデータの端部拡張(End Extension)の手法を示すパラメータ(mode)を指定しています。"DB4" は、4つの消失モーメントがある Daubechies ウェーブレットを使用することを意味し、"Z" はデータの端部をゼロパディング(ゼロ詰め)することを意味します。
  • 4〜11行目に配列(x)の要素を指定しています。

出力結果

(本関数の詳細はc09ca のマニュアルページを参照)

この出力例をダウンロード
c09ca Example Program Results
 DWT :: Wavelet:    DB4 , End mode:      Z
 Input Data      x :
   1.0000   3.0000   5.0000   7.0000   6.0000   4.0000   5.0000   2.0000
   Approximation coefficients CA : 
   0.0011  -0.0043  -0.0174   4.4778   8.9557   7.3401   2.5816
   Detail coefficients        CD : 
   0.0237   0.0410  -0.5966   1.7763  -0.7517   0.3332  -0.1188
   Reconstruction              Y : 
   1.0000   3.0000   5.0000   7.0000   6.0000   4.0000   5.0000   2.0000

  • 2行目にウェーブレット変換の手法とデータの端部拡張(End Extension)のモードが出力されています。
  • 4行目に入力データである配列の要素が出力されています。
  • 6行目に近似係数が出力されています。
  • 9行目に詳細係数が出力されています。
  • 10行目にウェーブレット再構成が出力されています。

ソースコード

(本関数の詳細はc09ca のマニュアルページを参照)

※本サンプルソースコードは .NET環境用の科学技術・統計計算ライブラリである「nAG Library for .NET」の関数を呼び出します。
サンプルのコンパイル及び実行方法


このソースコードをダウンロード
//      c09ca Example Program Text
//      C# version, nAG Copyright 2008
using System;
using NagLibrary;
namespace NagDotNetExamples
{
  public class C09CAE
  {
    static bool defaultdata = true;
    static string datafile = "";
    static void Main(String[] args)
    {
      if (args.Length == 1)
      {
        defaultdata = false;
        datafile = args[0];
      }
      StartExample();
    }
    public static void StartExample()
    {
      try
      {
        DataReader sr = null;
        if (defaultdata)
        {
          sr = new DataReader("exampledata/c09cae.d");
        }
        else
        {
          sr = new DataReader(datafile);
        }
        int i,   n,   nwc,   ny; string wtrans="";
        int ifail;
        Console.WriteLine("c09ca Example Program Results");
        //      Skip heading in data file
        sr.Reset();
        //      Read N
        sr.Reset();
        n = int.Parse(sr.Next());
        //      Read wavnam, mode
        sr.Reset();
        string wavnam = sr.Next();
        string mode = sr.Next();
        // 
        double[] x = new double[n];
        double[] y = new double[n];
        if (n >= 2)
        {
          Console.WriteLine(" {0} {1,5} {2} {3,5}","DWT :: Wavelet: ",wavnam,", End mode: ",mode);
          //         Read array
          sr.Reset();
          for (i = 1 ; i <= n ; i++)
          {
            x[i - 1] = double.Parse(sr.Next());
          }
          Console.WriteLine(" {0}","Input Data      x :");
          for (i = 1 ; i <= n ; i++)
          {
            Console.Write(" {0, 8:f4}", x[i - 1]);
          }
          Console.WriteLine();
          //         Query wavelet filter dimensions
          wtrans = "S";
          // 
          C09.C09Communications c09comm = new C09.C09Communications(wavnam, wtrans, mode, n,
          out ifail);
          nwc = c09comm.ApproximateCoeffCount;
          // 
          if (ifail != 0)
          {
            Console.WriteLine(" ** C09Communications constructor with ifail = {0, 3}", ifail);
            return;
          }
          // 
          double[] ca = new double[nwc];
          double[] cd = new double[nwc];
          C09.c09ca(n, x, ca, cd, c09comm, out ifail);
          // 
          if (ifail == 0)
          {
            Console.WriteLine("   Approximation coefficients CA : ");
            for (i = 1 ; i <= nwc ; i++)
            {
              Console.Write(" {0, 8:f4}", ca[i - 1]);
            }
            Console.WriteLine();
            Console.WriteLine("   Detail coefficients        CD : ");
            for (i = 1 ; i <= nwc ; i++)
            {
              Console.Write(" {0, 8:f4}", cd[i - 1]);
            }
            Console.WriteLine();
          }
          else
          {
            Console.WriteLine("");
            Console.WriteLine(" ** c09ca returned with ifail = {0, 3}", ifail);
          }
          if (mode == "PE")
          {
            ny = 2 * nwc;
          }
          else
          {
            ny = (int)n;
          }
          // 
          // 
          C09.c09cb(ca, cd, ny, y, c09comm, out ifail);
          // 
          if (ifail == 0)
          {
            Console.WriteLine("   Reconstruction              Y : ");
            for (i = 1 ; i <= ny ; i++)
            {
              Console.Write(" {0, 8:f4}", y[i - 1]);
            }
            Console.WriteLine();
          }
          else
          {
            Console.WriteLine(" {0} {1}","Error in c09cb: ifail = ",ifail);
          }
          // 
        }
        // 
      }
      catch (Exception e)
      {
        Console.WriteLine(e.Message);
        Console.WriteLine("Exception Raised");
      }
    }
  }
}


関連情報
Privacy Policy  /  Trademarks