Advertisement
Guest User

Untitled

a guest
Jan 18th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. /*
  2. This file is part of a program that implements a Software-Defined Radio.
  3.  
  4. Copyright (C) 2007, 2008 Philip A Covington
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. The author can be reached by email at
  21.  
  22. p.covington@gmail.com
  23.  
  24. */
  25.  
  26. #define WIN32
  27.  
  28. using System;
  29. using System.Runtime.InteropServices;
  30. using System.Security;
  31.  
  32. namespace SharpDSP2.Lite
  33. {
  34.        
  35.     #region FFTW
  36.     internal class FFTW
  37.     {
  38.         private const string library_name = "libfftw3f.dll";       
  39.        
  40.         internal enum fftw_direction : int
  41.         {
  42.             Forward=-1,
  43.             Backward=1
  44.         }      
  45.        
  46.         [Flags]
  47.         internal enum fftw_flags : uint
  48.         {      
  49.             Measure=0,     
  50.             DestroyInput=1,    
  51.             Unaligned=2,       
  52.             ConserveMemory=4,      
  53.             Exhaustive=8,      
  54.             PreserveInput=16,      
  55.             Patient=32,    
  56.             Estimate=64
  57.         }
  58.        
  59.         [DllImport(library_name,
  60.         EntryPoint = "fftwf_destroy_plan",
  61.         ExactSpelling = true),
  62.         System.Security.SuppressUnmanagedCodeSecurity]
  63.         internal static extern void destroy_plan(IntPtr plan);
  64.        
  65.         [DllImport(library_name,
  66.         EntryPoint = "fftwf_cleanup",
  67.         ExactSpelling = true),
  68.         System.Security.SuppressUnmanagedCodeSecurity]
  69.         internal static extern void cleanup();
  70.        
  71.         [DllImport(library_name,
  72.         EntryPoint = "fftwf_execute",
  73.         ExactSpelling = true),
  74.         System.Security.SuppressUnmanagedCodeSecurity]
  75.         internal static extern void execute(IntPtr plan);
  76.        
  77.         [DllImport(library_name,
  78.         EntryPoint = "fftwf_plan_dft_1d",
  79.         ExactSpelling = true),
  80.         System.Security.SuppressUnmanagedCodeSecurity]
  81.         internal static extern IntPtr dft_1d(int n, IntPtr input, IntPtr output,
  82.         fftw_direction direction, fftw_flags flags);
  83.     }
  84.    
  85.     #endregion
  86.    
  87.     /// <summary>
  88.     /// The DSP Buffer
  89.     /// </summary>
  90.    
  91.     [Serializable()]
  92.     public class DSPBuffer : IDisposable
  93.     {
  94.         #region Public Members
  95.         public SharpDSP2._1.CPX[] buf_in;
  96.         public SharpDSP2._1.CPX[] buf_out;
  97.         #endregion
  98.        
  99.         #region Private Members
  100.        
  101.         private IntPtr plan_fwd_main;
  102.         private IntPtr plan_rev_main;
  103.         private GCHandle h_buf_in;
  104.         private GCHandle h_buf_out;
  105.        
  106.         private readonly int size;
  107.         private readonly float scalefactor;
  108.        
  109.         #endregion
  110.        
  111.         #region Constructor
  112.        
  113.         public DSPBuffer(int fftSize)
  114.         {
  115.             size = fftSize;
  116.  
  117.             buf_in = new SharpDSP2._1.CPX[size];
  118.             buf_out = new SharpDSP2._1.CPX[size];
  119.            
  120.             scalefactor = (float)1.0f/size;
  121.            
  122.             h_buf_in = GCHandle.Alloc(buf_in, GCHandleType.Pinned);
  123.             h_buf_out = GCHandle.Alloc(buf_out, GCHandleType.Pinned);
  124.            
  125.             // in: cpx out: tmp_cpx_1
  126.             plan_fwd_main = FFTW.dft_1d(size,
  127.                                         h_buf_in.AddrOfPinnedObject(),
  128.                                         h_buf_out.AddrOfPinnedObject(),
  129.                                         FFTW.fftw_direction.Forward,
  130.                                         FFTW.fftw_flags.Measure);
  131.            
  132.             // in: tmp_cpx_2 out: tmp_cpx_3
  133.             plan_rev_main = FFTW.dft_1d(size,
  134.                                         h_buf_in.AddrOfPinnedObject(),
  135.                                         h_buf_out.AddrOfPinnedObject(),
  136.                                         FFTW.fftw_direction.Backward,
  137.                                         FFTW.fftw_flags.Measure);          
  138.         }
  139.  
  140.         ~DSPBuffer()
  141.         {
  142.             Dispose(true);
  143.         }
  144.  
  145.         public void Dispose()
  146.         {
  147.             Dispose(false);
  148.             GC.SuppressFinalize(this);
  149.         }
  150.  
  151.         private void Dispose(bool bDestructing) // destructor
  152.         {
  153.             FFTW.destroy_plan(plan_fwd_main);
  154.             FFTW.destroy_plan(plan_rev_main);
  155.             h_buf_in.Free();
  156.             h_buf_out.Free();
  157.         }
  158.        
  159.         #endregion
  160.        
  161.         #region Public Methods
  162.        
  163.         public void ClearAll()
  164.         {
  165.             Array.Clear(buf_in, 0, size);
  166.             Array.Clear(buf_out, 0, size);
  167.         }
  168.  
  169.         public float GetMagnitude(int index)
  170.         {
  171.             return (float)Math.Sqrt(buf_out[index].real *
  172.                                     buf_out[index].real +
  173.                                     buf_out[index].imag *
  174.                                     buf_out[index].imag);
  175.  
  176.             //if (Math.Abs(cpx[index].real) > Math.Abs(cpx[index].imag))
  177.             //    return (float)(Math.Abs(cpx[index].real) + 0.4 * Math.Abs(cpx[index].imag));
  178.             //else
  179.             //    return (float)(Math.Abs(cpx[index].imag) + 0.4 * Math.Abs(cpx[index].real));
  180.         }
  181.  
  182.         public void DoFFTForwardMain()
  183.         {
  184.             FFTW.execute(plan_fwd_main);
  185.         }
  186.                        
  187.         public void DoFFTReverseMain()
  188.         {
  189.             FFTW.execute(plan_rev_main);           
  190.         }
  191.                                            
  192.         public void NormalizeOut()
  193.         {
  194.             for (int i = 0; i < size; i++)
  195.             {
  196.                 buf_out[i].real *= this.scalefactor;
  197.                 buf_out[i].imag *= this.scalefactor;
  198.             }
  199.         }
  200.        
  201.         #endregion
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement