Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This file is part of a program that implements a Software-Defined Radio.
- Copyright (C) 2007, 2008 Philip A Covington
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- The author can be reached by email at
- p.covington@gmail.com
- */
- #define WIN32
- using System;
- using System.Runtime.InteropServices;
- using System.Security;
- namespace SharpDSP2.Lite
- {
- #region FFTW
- internal class FFTW
- {
- private const string library_name = "libfftw3f.dll";
- internal enum fftw_direction : int
- {
- Forward=-1,
- Backward=1
- }
- [Flags]
- internal enum fftw_flags : uint
- {
- Measure=0,
- DestroyInput=1,
- Unaligned=2,
- ConserveMemory=4,
- Exhaustive=8,
- PreserveInput=16,
- Patient=32,
- Estimate=64
- }
- [DllImport(library_name,
- EntryPoint = "fftwf_destroy_plan",
- ExactSpelling = true),
- System.Security.SuppressUnmanagedCodeSecurity]
- internal static extern void destroy_plan(IntPtr plan);
- [DllImport(library_name,
- EntryPoint = "fftwf_cleanup",
- ExactSpelling = true),
- System.Security.SuppressUnmanagedCodeSecurity]
- internal static extern void cleanup();
- [DllImport(library_name,
- EntryPoint = "fftwf_execute",
- ExactSpelling = true),
- System.Security.SuppressUnmanagedCodeSecurity]
- internal static extern void execute(IntPtr plan);
- [DllImport(library_name,
- EntryPoint = "fftwf_plan_dft_1d",
- ExactSpelling = true),
- System.Security.SuppressUnmanagedCodeSecurity]
- internal static extern IntPtr dft_1d(int n, IntPtr input, IntPtr output,
- fftw_direction direction, fftw_flags flags);
- }
- #endregion
- /// <summary>
- /// The DSP Buffer
- /// </summary>
- [Serializable()]
- public class DSPBuffer : IDisposable
- {
- #region Public Members
- public SharpDSP2._1.CPX[] buf_in;
- public SharpDSP2._1.CPX[] buf_out;
- #endregion
- #region Private Members
- private IntPtr plan_fwd_main;
- private IntPtr plan_rev_main;
- private GCHandle h_buf_in;
- private GCHandle h_buf_out;
- private readonly int size;
- private readonly float scalefactor;
- #endregion
- #region Constructor
- public DSPBuffer(int fftSize)
- {
- size = fftSize;
- buf_in = new SharpDSP2._1.CPX[size];
- buf_out = new SharpDSP2._1.CPX[size];
- scalefactor = (float)1.0f/size;
- h_buf_in = GCHandle.Alloc(buf_in, GCHandleType.Pinned);
- h_buf_out = GCHandle.Alloc(buf_out, GCHandleType.Pinned);
- // in: cpx out: tmp_cpx_1
- plan_fwd_main = FFTW.dft_1d(size,
- h_buf_in.AddrOfPinnedObject(),
- h_buf_out.AddrOfPinnedObject(),
- FFTW.fftw_direction.Forward,
- FFTW.fftw_flags.Measure);
- // in: tmp_cpx_2 out: tmp_cpx_3
- plan_rev_main = FFTW.dft_1d(size,
- h_buf_in.AddrOfPinnedObject(),
- h_buf_out.AddrOfPinnedObject(),
- FFTW.fftw_direction.Backward,
- FFTW.fftw_flags.Measure);
- }
- ~DSPBuffer()
- {
- Dispose(true);
- }
- public void Dispose()
- {
- Dispose(false);
- GC.SuppressFinalize(this);
- }
- private void Dispose(bool bDestructing) // destructor
- {
- FFTW.destroy_plan(plan_fwd_main);
- FFTW.destroy_plan(plan_rev_main);
- h_buf_in.Free();
- h_buf_out.Free();
- }
- #endregion
- #region Public Methods
- public void ClearAll()
- {
- Array.Clear(buf_in, 0, size);
- Array.Clear(buf_out, 0, size);
- }
- public float GetMagnitude(int index)
- {
- return (float)Math.Sqrt(buf_out[index].real *
- buf_out[index].real +
- buf_out[index].imag *
- buf_out[index].imag);
- //if (Math.Abs(cpx[index].real) > Math.Abs(cpx[index].imag))
- // return (float)(Math.Abs(cpx[index].real) + 0.4 * Math.Abs(cpx[index].imag));
- //else
- // return (float)(Math.Abs(cpx[index].imag) + 0.4 * Math.Abs(cpx[index].real));
- }
- public void DoFFTForwardMain()
- {
- FFTW.execute(plan_fwd_main);
- }
- public void DoFFTReverseMain()
- {
- FFTW.execute(plan_rev_main);
- }
- public void NormalizeOut()
- {
- for (int i = 0; i < size; i++)
- {
- buf_out[i].real *= this.scalefactor;
- buf_out[i].imag *= this.scalefactor;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement