Guest User

Untitled

a guest
Jan 18th, 2014
129
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.  
  23. */
  24.  
  25. #define WIN32
  26.  
  27. using System;
  28. using System.Runtime.InteropServices;
  29. using System.Security;
  30.  
  31. namespace SharpDSP2.Lite
  32. {
  33.        
  34.     #region FFTW
  35.     internal class FFTW
  36.     {
  37.         private const string library_name = "libfftw3f.dll";       
  38.        
  39.         internal enum fftw_direction : int
  40.         {
  41.             Forward=-1,
  42.             Backward=1
  43.         }      
  44.        
  45.         [Flags]
  46.         internal enum fftw_flags : uint
  47.         {      
  48.             Measure=0,     
  49.             DestroyInput=1,    
  50.             Unaligned=2,       
  51.             ConserveMemory=4,      
  52.             Exhaustive=8,      
  53.             PreserveInput=16,      
  54.             Patient=32,    
  55.             Estimate=64
  56.         }
  57.        
  58.         [DllImport(library_name,
  59.         EntryPoint = "fftwf_destroy_plan",
  60.         ExactSpelling = true),
  61.         System.Security.SuppressUnmanagedCodeSecurity]
  62.         internal static extern void destroy_plan(IntPtr plan);
  63.        
  64.         [DllImport(library_name,
  65.         EntryPoint = "fftwf_cleanup",
  66.         ExactSpelling = true),
  67.         System.Security.SuppressUnmanagedCodeSecurity]
  68.         internal static extern void cleanup();
  69.        
  70.         [DllImport(library_name,
  71.         EntryPoint = "fftwf_execute",
  72.         ExactSpelling = true),
  73.         System.Security.SuppressUnmanagedCodeSecurity]
  74.         internal static extern void execute(IntPtr plan);
  75.        
  76.         [DllImport(library_name,
  77.         EntryPoint = "fftwf_plan_dft_1d",
  78.         ExactSpelling = true),
  79.         System.Security.SuppressUnmanagedCodeSecurity]
  80.         internal static extern IntPtr dft_1d(int n, IntPtr input, IntPtr output,
  81.         fftw_direction direction, fftw_flags flags);
  82.     }
  83.    
  84.     #endregion
  85.    
  86.     /// <summary>
  87.     /// The DSP Buffer
  88.     /// </summary>
  89.    
  90.     [Serializable()]
  91.     public class DSPBuffer : IDisposable
  92.     {
  93.         #region Public Members
  94.         public SharpDSP2._1.CPX[] buf_in;
  95.         public SharpDSP2._1.CPX[] buf_out;
  96.         #endregion
  97.        
  98.         #region Private Members
  99.        
  100.         private IntPtr plan_fwd_main;
  101.         private IntPtr plan_rev_main;
  102.         private GCHandle h_buf_in;
  103.         private GCHandle h_buf_out;
  104.        
  105.         private readonly int size;
  106.         private readonly float scalefactor;
  107.        
  108.         #endregion
  109.        
  110.         #region Constructor
  111.        
  112.         public DSPBuffer(int fftSize)
  113.         {
  114.             size = fftSize;
  115.  
  116.             buf_in = new SharpDSP2._1.CPX[size];
  117.             buf_out = new SharpDSP2._1.CPX[size];
  118.            
  119.             scalefactor = (float)1.0f/size;
  120.            
  121.             h_buf_in = GCHandle.Alloc(buf_in, GCHandleType.Pinned);
  122.             h_buf_out = GCHandle.Alloc(buf_out, GCHandleType.Pinned);
  123.            
  124.             // in: cpx out: tmp_cpx_1
  125.             plan_fwd_main = FFTW.dft_1d(size,
  126.                                         h_buf_in.AddrOfPinnedObject(),
  127.                                         h_buf_out.AddrOfPinnedObject(),
  128.                                         FFTW.fftw_direction.Forward,
  129.                                         FFTW.fftw_flags.Measure);
  130.            
  131.             // in: tmp_cpx_2 out: tmp_cpx_3
  132.             plan_rev_main = FFTW.dft_1d(size,
  133.                                         h_buf_in.AddrOfPinnedObject(),
  134.                                         h_buf_out.AddrOfPinnedObject(),
  135.                                         FFTW.fftw_direction.Backward,
  136.                                         FFTW.fftw_flags.Measure);          
  137.         }
  138.  
  139.         ~DSPBuffer()
  140.         {
  141.             Dispose(true);
  142.         }
  143.  
  144.         public void Dispose()
  145.         {
  146.             Dispose(false);
  147.             GC.SuppressFinalize(this);
  148.         }
  149.  
  150.         private void Dispose(bool bDestructing) // destructor
  151.         {
  152.             FFTW.destroy_plan(plan_fwd_main);
  153.             FFTW.destroy_plan(plan_rev_main);
  154.             h_buf_in.Free();
  155.             h_buf_out.Free();
  156.         }
  157.        
  158.         #endregion
  159.        
  160.         #region Public Methods
  161.        
  162.         public void ClearAll()
  163.         {
  164.             Array.Clear(buf_in, 0, size);
  165.             Array.Clear(buf_out, 0, size);
  166.         }
  167.  
  168.         public float GetMagnitude(int index)
  169.         {
  170.             return (float)Math.Sqrt(buf_out[index].real *
  171.                                     buf_out[index].real +
  172.                                     buf_out[index].imag *
  173.                                     buf_out[index].imag);
  174.  
  175.             //if (Math.Abs(cpx[index].real) > Math.Abs(cpx[index].imag))
  176.             //    return (float)(Math.Abs(cpx[index].real) + 0.4 * Math.Abs(cpx[index].imag));
  177.             //else
  178.             //    return (float)(Math.Abs(cpx[index].imag) + 0.4 * Math.Abs(cpx[index].real));
  179.         }
  180.  
  181.         public void DoFFTForwardMain()
  182.         {
  183.             FFTW.execute(plan_fwd_main);
  184.         }
  185.                        
  186.         public void DoFFTReverseMain()
  187.         {
  188.             FFTW.execute(plan_rev_main);           
  189.         }
  190.                                            
  191.         public void NormalizeOut()
  192.         {
  193.             for (int i = 0; i < size; i++)
  194.             {
  195.                 buf_out[i].real *= this.scalefactor;
  196.                 buf_out[i].imag *= this.scalefactor;
  197.             }
  198.         }
  199.        
  200.         #endregion
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment