Swift Linear Interpolation and UpSampling











up vote
0
down vote

favorite
1












I have a stream of metrics that are unevenly sampled. I want to linearly interpolate and upsample these metrics to a specific sampling frequency. I have tried to use the Accelerate Framework and the SIMD framework but I am not really sure what to do.



The problem itself is as follows:



let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]


So I am looking for a way to find the new_values through some sort of linear interpolation method.










share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    I have a stream of metrics that are unevenly sampled. I want to linearly interpolate and upsample these metrics to a specific sampling frequency. I have tried to use the Accelerate Framework and the SIMD framework but I am not really sure what to do.



    The problem itself is as follows:



    let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
    let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
    let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]


    So I am looking for a way to find the new_values through some sort of linear interpolation method.










    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have a stream of metrics that are unevenly sampled. I want to linearly interpolate and upsample these metrics to a specific sampling frequency. I have tried to use the Accelerate Framework and the SIMD framework but I am not really sure what to do.



      The problem itself is as follows:



      let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
      let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
      let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]


      So I am looking for a way to find the new_values through some sort of linear interpolation method.










      share|improve this question













      I have a stream of metrics that are unevenly sampled. I want to linearly interpolate and upsample these metrics to a specific sampling frequency. I have tried to use the Accelerate Framework and the SIMD framework but I am not really sure what to do.



      The problem itself is as follows:



      let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
      let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
      let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]


      So I am looking for a way to find the new_values through some sort of linear interpolation method.







      arrays swift interpolation accelerate-framework linear-interpolation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 16:54









      Samyak Shah

      83




      83
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          vDSP_vgenpD will do the job for you. Pass it the original times and values, and it will populate an array with the interpolated values. For example:



          import Accelerate

          let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
          let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]

          var new_values = [Double](repeating: 0,
          count: 11)

          let stride = vDSP_Stride(1)

          vDSP_vgenpD(original_values, stride,
          original_times, stride,
          &new_values, stride,
          vDSP_Length(new_values.count),
          vDSP_Length(original_values.count))


          You can get an array of time / value tuples with:



          let result = new_values.enumerated().map{ return $0 }


          That looks like:



          enter image description here






          share|improve this answer






























            up vote
            0
            down vote













            Interpolation is a wide field (see. Wikipedia : https://en.wikipedia.org/wiki/Interpolation)



            The simplest method is a linear interpolation like this.



            class LinearInterpolation {

            private var n : Int
            private var x : [Double]
            private var y : [Double]
            init (x: [Double], y: [Double]) {
            assert(x.count == y.count)
            self.n = x.count-1
            self.x = x
            self.y = y
            }

            func Interpolate(t: Double) -> Double {
            if t <= x[0] { return y[0] }
            for i in 1...n {
            if t <= x[i] {
            let ans = (t-x[i-1]) * (y[i] - y[i-1]) / (x[i]-x[i-1]) + y[i-1]
            return ans
            }
            }
            return y[n]
            }


            }



            Usage:



                let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
            let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
            let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
            let ipol = LinearInterpolation(x: original_times, y: original_values)
            for t in new_times {
            let y = ipol.Interpolate(t: t)
            print("t: (t) y: (y)")
            }


            In your Usecase with something like audio data you should take a look at Fourier analysis.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212548%2fswift-linear-interpolation-and-upsampling%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              vDSP_vgenpD will do the job for you. Pass it the original times and values, and it will populate an array with the interpolated values. For example:



              import Accelerate

              let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
              let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]

              var new_values = [Double](repeating: 0,
              count: 11)

              let stride = vDSP_Stride(1)

              vDSP_vgenpD(original_values, stride,
              original_times, stride,
              &new_values, stride,
              vDSP_Length(new_values.count),
              vDSP_Length(original_values.count))


              You can get an array of time / value tuples with:



              let result = new_values.enumerated().map{ return $0 }


              That looks like:



              enter image description here






              share|improve this answer



























                up vote
                1
                down vote



                accepted










                vDSP_vgenpD will do the job for you. Pass it the original times and values, and it will populate an array with the interpolated values. For example:



                import Accelerate

                let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]

                var new_values = [Double](repeating: 0,
                count: 11)

                let stride = vDSP_Stride(1)

                vDSP_vgenpD(original_values, stride,
                original_times, stride,
                &new_values, stride,
                vDSP_Length(new_values.count),
                vDSP_Length(original_values.count))


                You can get an array of time / value tuples with:



                let result = new_values.enumerated().map{ return $0 }


                That looks like:



                enter image description here






                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  vDSP_vgenpD will do the job for you. Pass it the original times and values, and it will populate an array with the interpolated values. For example:



                  import Accelerate

                  let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                  let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]

                  var new_values = [Double](repeating: 0,
                  count: 11)

                  let stride = vDSP_Stride(1)

                  vDSP_vgenpD(original_values, stride,
                  original_times, stride,
                  &new_values, stride,
                  vDSP_Length(new_values.count),
                  vDSP_Length(original_values.count))


                  You can get an array of time / value tuples with:



                  let result = new_values.enumerated().map{ return $0 }


                  That looks like:



                  enter image description here






                  share|improve this answer














                  vDSP_vgenpD will do the job for you. Pass it the original times and values, and it will populate an array with the interpolated values. For example:



                  import Accelerate

                  let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                  let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]

                  var new_values = [Double](repeating: 0,
                  count: 11)

                  let stride = vDSP_Stride(1)

                  vDSP_vgenpD(original_values, stride,
                  original_times, stride,
                  &new_values, stride,
                  vDSP_Length(new_values.count),
                  vDSP_Length(original_values.count))


                  You can get an array of time / value tuples with:



                  let result = new_values.enumerated().map{ return $0 }


                  That looks like:



                  enter image description here







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 at 15:07

























                  answered Nov 15 at 9:29









                  Simon Gladman

                  2,3291014




                  2,3291014
























                      up vote
                      0
                      down vote













                      Interpolation is a wide field (see. Wikipedia : https://en.wikipedia.org/wiki/Interpolation)



                      The simplest method is a linear interpolation like this.



                      class LinearInterpolation {

                      private var n : Int
                      private var x : [Double]
                      private var y : [Double]
                      init (x: [Double], y: [Double]) {
                      assert(x.count == y.count)
                      self.n = x.count-1
                      self.x = x
                      self.y = y
                      }

                      func Interpolate(t: Double) -> Double {
                      if t <= x[0] { return y[0] }
                      for i in 1...n {
                      if t <= x[i] {
                      let ans = (t-x[i-1]) * (y[i] - y[i-1]) / (x[i]-x[i-1]) + y[i-1]
                      return ans
                      }
                      }
                      return y[n]
                      }


                      }



                      Usage:



                          let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                      let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
                      let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
                      let ipol = LinearInterpolation(x: original_times, y: original_values)
                      for t in new_times {
                      let y = ipol.Interpolate(t: t)
                      print("t: (t) y: (y)")
                      }


                      In your Usecase with something like audio data you should take a look at Fourier analysis.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Interpolation is a wide field (see. Wikipedia : https://en.wikipedia.org/wiki/Interpolation)



                        The simplest method is a linear interpolation like this.



                        class LinearInterpolation {

                        private var n : Int
                        private var x : [Double]
                        private var y : [Double]
                        init (x: [Double], y: [Double]) {
                        assert(x.count == y.count)
                        self.n = x.count-1
                        self.x = x
                        self.y = y
                        }

                        func Interpolate(t: Double) -> Double {
                        if t <= x[0] { return y[0] }
                        for i in 1...n {
                        if t <= x[i] {
                        let ans = (t-x[i-1]) * (y[i] - y[i-1]) / (x[i]-x[i-1]) + y[i-1]
                        return ans
                        }
                        }
                        return y[n]
                        }


                        }



                        Usage:



                            let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                        let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
                        let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
                        let ipol = LinearInterpolation(x: original_times, y: original_values)
                        for t in new_times {
                        let y = ipol.Interpolate(t: t)
                        print("t: (t) y: (y)")
                        }


                        In your Usecase with something like audio data you should take a look at Fourier analysis.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Interpolation is a wide field (see. Wikipedia : https://en.wikipedia.org/wiki/Interpolation)



                          The simplest method is a linear interpolation like this.



                          class LinearInterpolation {

                          private var n : Int
                          private var x : [Double]
                          private var y : [Double]
                          init (x: [Double], y: [Double]) {
                          assert(x.count == y.count)
                          self.n = x.count-1
                          self.x = x
                          self.y = y
                          }

                          func Interpolate(t: Double) -> Double {
                          if t <= x[0] { return y[0] }
                          for i in 1...n {
                          if t <= x[i] {
                          let ans = (t-x[i-1]) * (y[i] - y[i-1]) / (x[i]-x[i-1]) + y[i-1]
                          return ans
                          }
                          }
                          return y[n]
                          }


                          }



                          Usage:



                              let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                          let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
                          let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
                          let ipol = LinearInterpolation(x: original_times, y: original_values)
                          for t in new_times {
                          let y = ipol.Interpolate(t: t)
                          print("t: (t) y: (y)")
                          }


                          In your Usecase with something like audio data you should take a look at Fourier analysis.






                          share|improve this answer












                          Interpolation is a wide field (see. Wikipedia : https://en.wikipedia.org/wiki/Interpolation)



                          The simplest method is a linear interpolation like this.



                          class LinearInterpolation {

                          private var n : Int
                          private var x : [Double]
                          private var y : [Double]
                          init (x: [Double], y: [Double]) {
                          assert(x.count == y.count)
                          self.n = x.count-1
                          self.x = x
                          self.y = y
                          }

                          func Interpolate(t: Double) -> Double {
                          if t <= x[0] { return y[0] }
                          for i in 1...n {
                          if t <= x[i] {
                          let ans = (t-x[i-1]) * (y[i] - y[i-1]) / (x[i]-x[i-1]) + y[i-1]
                          return ans
                          }
                          }
                          return y[n]
                          }


                          }



                          Usage:



                              let original_times:[Double] = [0.0, 2.0, 3.0, 6.0, 10.0]
                          let original_values: [Double] = [50.0, 20.0, 30.0, 40.0, 10.0]
                          let new_times:[Double] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
                          let ipol = LinearInterpolation(x: original_times, y: original_values)
                          for t in new_times {
                          let y = ipol.Interpolate(t: t)
                          print("t: (t) y: (y)")
                          }


                          In your Usecase with something like audio data you should take a look at Fourier analysis.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 17:55









                          Stephan Januar

                          713




                          713






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212548%2fswift-linear-interpolation-and-upsampling%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Schultheiß

                              Liste der Kulturdenkmale in Wilsdruff

                              Android Play Services Check