Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Will casting / passing 16 bit int as double harm FFT processing?

Will casting / passing 16 bit int as double harm FFT processing?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
14 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rick York

    I am not sure what you mean by "do much damage to." You can use a cast and pass the values as doubles but it will slow things down somewhat.

    V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #3

    Thanks. Speed penalty is immaterial since doing FFT is relatively "time consuming" already. What I met was - will the FFT be reasonably accurate giving the (Arduino) processor hardware constrains. I guess I'll know after I reconstitute known signal. Cheers Vaclav

    1 Reply Last reply
    0
    • V Vaclav_

      I am getting back into “real coding” and as usual have academic question, I hope. I am collecting raw data from 10 bits A/D - in uint16_t. The FFT class I am using uses double to calculate stuff. My silly question is – should I build overloaded function(s) using double type of variable or can I just cast the raw data as (double) function parameter? Will casting do much damage to FFT processing? Since I don't know how the processed data should look like, I need to make sure using casts is OK or not.. Thanks for your time and help Cheers Vaclav

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      You just need to convert the values by simple expressions of the form:

      double doubleValue = integerValue;

      Whether you do this before calling your function or within it is up to you.

      V 1 Reply Last reply
      0
      • L Lost User

        You just need to convert the values by simple expressions of the form:

        double doubleValue = integerValue;

        Whether you do this before calling your function or within it is up to you.

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #5

        So casting is a bad idea? What exactly is the difference between cast and redefinition? As a secondary question - the initial raw values are in an array in RAM. I pass a pointer to this int array either by redefining it as double or as a cast. I am not far enough it the coding , but I think the raw array is processed / modified. How does it affect the original RAM size? I assume it it still intact as int array, not a double. Microprocessor has limited RAM and to process the signal using FFT the original raw data array needs to be as large as feasible ( right now I am testing the code using 64 "samples" ) - to fit it into RAM plus processed data by FFT. Thanks for your help. Cheers Vaclav

        L 1 Reply Last reply
        0
        • V Vaclav_

          So casting is a bad idea? What exactly is the difference between cast and redefinition? As a secondary question - the initial raw values are in an array in RAM. I pass a pointer to this int array either by redefining it as double or as a cast. I am not far enough it the coding , but I think the raw array is processed / modified. How does it affect the original RAM size? I assume it it still intact as int array, not a double. Microprocessor has limited RAM and to process the signal using FFT the original raw data array needs to be as large as feasible ( right now I am testing the code using 64 "samples" ) - to fit it into RAM plus processed data by FFT. Thanks for your help. Cheers Vaclav

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Vaclav_Sal wrote:

          So casting is a bad idea?

          Not at all, but you need to understand what you are doing when you use it. A cast is used to tell the compiler that you want to treat one object type as another, and that you understand the potential side effects.

          Vaclav_Sal wrote:

          What exactly is the difference between cast and redefinition?

          Not sure I understand what you mean by "redefinition" in this context.

          Vaclav_Sal wrote:

          I pass a pointer to this int array either by redefining it as double or as a cast.

          That does not make sense, you cannot use a cast to pretend that an array of integers is an array of doubles, they are totally different types.

          V 1 Reply Last reply
          0
          • L Lost User

            Vaclav_Sal wrote:

            So casting is a bad idea?

            Not at all, but you need to understand what you are doing when you use it. A cast is used to tell the compiler that you want to treat one object type as another, and that you understand the potential side effects.

            Vaclav_Sal wrote:

            What exactly is the difference between cast and redefinition?

            Not sure I understand what you mean by "redefinition" in this context.

            Vaclav_Sal wrote:

            I pass a pointer to this int array either by redefining it as double or as a cast.

            That does not make sense, you cannot use a cast to pretend that an array of integers is an array of doubles, they are totally different types.

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #7

            Richard, I read it as you have redefined int variable as double variable. OK, this is getting into "what if" area. I am going to build the raw data as double to start with and later on find a microprocessor with more RAM than this one so I do not have to limit the sample length. Here is yet another question - because the original data is 10 bits and I do not want to carry any garbage in unused bits - how do I mask the 10 bits into double ( 32 bits) variable? Which in these four 8 bits are the value and which are after the "decimal points" values? Sorry , I just do not know the proper mathematical English terms. Or maybe I just use "redefinition" from int to double on the result of the AD conversion, as you suggested, just KISS. BTW as I suspected - the original raw data gets processed few times over to various FFT data. I need to analyze the original code to find out how did the raw data started as int array and was processed into double. IMHO that would messed up the memory big time - unless the int array dimension is doubled to make room for the half sized double array. FFT results are "symetrical" , thus the "samples" are cut in half. Fun. Cheers Vaclav

            L 1 Reply Last reply
            0
            • V Vaclav_

              Richard, I read it as you have redefined int variable as double variable. OK, this is getting into "what if" area. I am going to build the raw data as double to start with and later on find a microprocessor with more RAM than this one so I do not have to limit the sample length. Here is yet another question - because the original data is 10 bits and I do not want to carry any garbage in unused bits - how do I mask the 10 bits into double ( 32 bits) variable? Which in these four 8 bits are the value and which are after the "decimal points" values? Sorry , I just do not know the proper mathematical English terms. Or maybe I just use "redefinition" from int to double on the result of the AD conversion, as you suggested, just KISS. BTW as I suspected - the original raw data gets processed few times over to various FFT data. I need to analyze the original code to find out how did the raw data started as int array and was processed into double. IMHO that would messed up the memory big time - unless the int array dimension is doubled to make room for the half sized double array. FFT results are "symetrical" , thus the "samples" are cut in half. Fun. Cheers Vaclav

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              No that was not a redefinition but a conversion.

              int integerValue = 233;
              double doubleValue = integerValue; // the integer value is converted to a double
              // and stored in the new variable

              I am getting a bit confused as to your raw data and what format it takes. If it is in integer form and you need to convert it to double values then you need to use expressions like the above to convert it. It's a long time since I did any serious maths, so forgive me if I struggle to understand your questions.

              V 1 Reply Last reply
              0
              • L Lost User

                No that was not a redefinition but a conversion.

                int integerValue = 233;
                double doubleValue = integerValue; // the integer value is converted to a double
                // and stored in the new variable

                I am getting a bit confused as to your raw data and what format it takes. If it is in integer form and you need to convert it to double values then you need to use expressions like the above to convert it. It's a long time since I did any serious maths, so forgive me if I struggle to understand your questions.

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #9

                Lets try this approach int integerValue = 233; Orignaly the integerValue valid data is 10 bits instead of full 16 It is in two 8 bits registers Only 10 bits are defined so I mask it with 0x3FF, that may be ovekill, but safe. double doubleValue = integerValue; // the integer value is converted to a double // and stored in the new variable I understand the above , I used wrong therm calling it conversion. My basic question is what is the difference between these function( doublevalue,... function( (double) integerValue,.... BTW the parameter passed is a pointer.

                L 1 Reply Last reply
                0
                • V Vaclav_

                  Lets try this approach int integerValue = 233; Orignaly the integerValue valid data is 10 bits instead of full 16 It is in two 8 bits registers Only 10 bits are defined so I mask it with 0x3FF, that may be ovekill, but safe. double doubleValue = integerValue; // the integer value is converted to a double // and stored in the new variable I understand the above , I used wrong therm calling it conversion. My basic question is what is the difference between these function( doublevalue,... function( (double) integerValue,.... BTW the parameter passed is a pointer.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  // the value passed in is already a double, or if not it will be converted
                  function( doublevalue,...

                  // the integer value will be converted into a double. But you do not need a cast.
                  function( (double) integerValue,....

                  However, if you are passing a pointer to the array of integers then you connot use anything like the above. You must pass it as an integer pointer and the called function will then do the conversion. Something like:

                  double function(int* myArrayOfIntegers, int count)
                  {
                  double result = 0.0;

                  for (int i = 0; i < count; ++i)
                  {
                      double temp = myArrayOfIntegers\[i\]; // get next integer and convert to double
                      // do some calculations
                  }
                  return result;
                  

                  }
                  // ....

                  // create an array to hold all the values
                  int* theIntegers = new int[theCount];
                  // fill the array from the source values
                  // ...

                  // send the array to be processed
                  double theAnswer = function(theIntegers, theCount);

                  V 1 Reply Last reply
                  0
                  • L Lost User

                    // the value passed in is already a double, or if not it will be converted
                    function( doublevalue,...

                    // the integer value will be converted into a double. But you do not need a cast.
                    function( (double) integerValue,....

                    However, if you are passing a pointer to the array of integers then you connot use anything like the above. You must pass it as an integer pointer and the called function will then do the conversion. Something like:

                    double function(int* myArrayOfIntegers, int count)
                    {
                    double result = 0.0;

                    for (int i = 0; i < count; ++i)
                    {
                        double temp = myArrayOfIntegers\[i\]; // get next integer and convert to double
                        // do some calculations
                    }
                    return result;
                    

                    }
                    // ....

                    // create an array to hold all the values
                    int* theIntegers = new int[theCount];
                    // fill the array from the source values
                    // ...

                    // send the array to be processed
                    double theAnswer = function(theIntegers, theCount);

                    V Offline
                    V Offline
                    Vaclav_
                    wrote on last edited by
                    #11

                    Hello Richard, this is getting really interesting. Here is some stuff to think about. It all started by compiler complaining about integer pointer being passed to function expecting double pointer. Second - I put together two sources - one collecting 10 bits of data into 16 bits integer array and not processing anything, just collecting. The second source builds / emulates sine wave into array of doubles and that gets processed by FFT. So I replaced the emulated (double) data with real (integer) collected data. My thinking is - if I cast or build a new variable as double it will take an 16 bit integer and process it as double, than what will happen to the original array ? I am still learning about FFT , but as far as I can tell the first FFT function puts new data (normal pointer behavior) into the original (integer ) array as what ? Integer? I don't think so. ( And how is the pointer advanced? If as double will it skip next ADC data?) I think the only way is to collect the ADC data into double array from get go. And that was the OP question. Thanks for all your help. Cheers Vaclav

                    L A 2 Replies Last reply
                    0
                    • V Vaclav_

                      Hello Richard, this is getting really interesting. Here is some stuff to think about. It all started by compiler complaining about integer pointer being passed to function expecting double pointer. Second - I put together two sources - one collecting 10 bits of data into 16 bits integer array and not processing anything, just collecting. The second source builds / emulates sine wave into array of doubles and that gets processed by FFT. So I replaced the emulated (double) data with real (integer) collected data. My thinking is - if I cast or build a new variable as double it will take an 16 bit integer and process it as double, than what will happen to the original array ? I am still learning about FFT , but as far as I can tell the first FFT function puts new data (normal pointer behavior) into the original (integer ) array as what ? Integer? I don't think so. ( And how is the pointer advanced? If as double will it skip next ADC data?) I think the only way is to collect the ADC data into double array from get go. And that was the OP question. Thanks for all your help. Cheers Vaclav

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Vaclav_Sal wrote:

                      this is getting really interesting.

                      Not really, it's pretty basic stuff. As I said before, you cannot send a pointer to an array of integers to a function that expects a pointer to an array of doubles. The types are totally different so your program would just be processing garbage. I showed you in my previous message how to pass the array of integers to the function that needs the values as doubles. That is all there is to it, the compiler will generate the correct code to convert each integer to a double as you process them. The resulting double values can then be used in your FFT calculations.

                      1 Reply Last reply
                      0
                      • V Vaclav_

                        Hello Richard, this is getting really interesting. Here is some stuff to think about. It all started by compiler complaining about integer pointer being passed to function expecting double pointer. Second - I put together two sources - one collecting 10 bits of data into 16 bits integer array and not processing anything, just collecting. The second source builds / emulates sine wave into array of doubles and that gets processed by FFT. So I replaced the emulated (double) data with real (integer) collected data. My thinking is - if I cast or build a new variable as double it will take an 16 bit integer and process it as double, than what will happen to the original array ? I am still learning about FFT , but as far as I can tell the first FFT function puts new data (normal pointer behavior) into the original (integer ) array as what ? Integer? I don't think so. ( And how is the pointer advanced? If as double will it skip next ADC data?) I think the only way is to collect the ADC data into double array from get go. And that was the OP question. Thanks for all your help. Cheers Vaclav

                        A Offline
                        A Offline
                        Albert Holguin
                        wrote on last edited by
                        #13

                        Vaclav_Sal wrote:

                        one collecting 10 bits of data into 16 bits integer array

                        This is common for transferring along byte boundaries. If you transmit 10bits, how many bytes are you moving across a network? ...yeah, integer numbers are desirable for network operations. Pretty much all receivers/digitizers do this.

                        Vaclav_Sal wrote:

                        if I cast or build a new variable as double it will take an 16 bit integer and process it as double, than what will happen to the original array ?

                        When casting, nothing happens to your original data, a copy is made. Although as Richard already mentioned, you can't simply cast a whole array, since your element spacing will be different (16bit vs 32bit array elements).

                        Vaclav_Sal wrote:

                        I am still learning about FFT , but as far as I can tell the first FFT function puts new data (normal pointer behavior)   into the original (integer ) array as what ? Integer?

                        Output of most FFT routines is a float or double (depending on whether it's "single" or "double" precision math).

                        Vaclav_Sal wrote:

                        I think the   only way is to collect the ADC data into double array from get go.

                        Not going to happen. Analog to digital converters (and most hardware for that matter) deal with integers (you'll commonly hear the term fixed point math, that's why).

                        V 1 Reply Last reply
                        0
                        • A Albert Holguin

                          Vaclav_Sal wrote:

                          one collecting 10 bits of data into 16 bits integer array

                          This is common for transferring along byte boundaries. If you transmit 10bits, how many bytes are you moving across a network? ...yeah, integer numbers are desirable for network operations. Pretty much all receivers/digitizers do this.

                          Vaclav_Sal wrote:

                          if I cast or build a new variable as double it will take an 16 bit integer and process it as double, than what will happen to the original array ?

                          When casting, nothing happens to your original data, a copy is made. Although as Richard already mentioned, you can't simply cast a whole array, since your element spacing will be different (16bit vs 32bit array elements).

                          Vaclav_Sal wrote:

                          I am still learning about FFT , but as far as I can tell the first FFT function puts new data (normal pointer behavior)   into the original (integer ) array as what ? Integer?

                          Output of most FFT routines is a float or double (depending on whether it's "single" or "double" precision math).

                          Vaclav_Sal wrote:

                          I think the   only way is to collect the ADC data into double array from get go.

                          Not going to happen. Analog to digital converters (and most hardware for that matter) deal with integers (you'll commonly hear the term fixed point math, that's why).

                          V Offline
                          V Offline
                          Vaclav_
                          wrote on last edited by
                          #14

                          Thanks for reply. Somehow I did not get the message across and the main question is getting muddled by stuff I am not so concerned about. The bottom line - which I actually just wanted someone to confirm - is that casting pointers from integer to double is a stupid idea. Thanks Case closed

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups