bugGNU Octave - Bugs: bug #61754, Bicubic interpolation in interp2()...


bug #61754: Bicubic interpolation in interp2() incompatible with Matlab

Submitted by:  Christof Kaufmann <christofkaufmann>
Submitted on:  Mon 03 Jan 2022 01:25:04 AM UTC  
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  None Assigned to:  None
Originator Name:  Christof Kaufmann Open/Closed:  Open
Release:  dev Operating System:  Any

Add a New Comment (Rich Markup)

You are not logged in

Please log in, so followups can be emailed to you.


( Jump to the original submission)

Sat 22 Jan 2022 10:26:11 AM UTC, comment #6: 

Rebased on new patch of bug #61863.

(file #52707)

Christof Kaufmann <christofkaufmann>
Thu 20 Jan 2022 11:03:23 AM UTC, comment #5: 

Sorry, I forgot to change the documentation about "cubic". Updated patch attached (still rebased onto the patch of bug #61863).

(file #52699)

Christof Kaufmann <christofkaufmann>
Thu 20 Jan 2022 02:46:56 AM UTC, comment #4: 

OK, I submitted a new bug report (bug #61863) for the complex matrix issue. While doing this, I found out, that "pchip" needed some extra care. I also added a patch there.

Then I rebased the bicubic interpolation fix on the patch of bug #61863 with some minor improvements compared to the submission in comment #2. The new patch is attached.

(file #52694)

Christof Kaufmann <christofkaufmann>
Wed 19 Jan 2022 04:54:11 PM UTC, comment #3: 

Thank you very much for your work.

Instead of modified source files, patch files are the preferred form of contribution. That way, it would be easier to see what you changed and it would reduce the risk of accidentally reverting changes in the development sources (by replacing an existing file with some other file).

Additionally, it would be preferable if bug fixes and new features come in distinct patch files. (You can still use the same bug report for submitting those when they are closely related.)

See also: https://wiki.octave.org/Contribute

Markus Mützel <mmuetzel>
Project Member
Wed 19 Jan 2022 04:43:51 PM UTC, comment #2: 

I implemented bicubic interpolation in interp2.m, see attached file. To make the implementation easier to understand, I also attached a PDF (LaTeX source) to explain the approach. I also added tests, not only for the bicubic interpolation, but for the sake of consistency also for the existing methods. I found a few bugs:

  • Interpolating complex matrixes gave an NA + 0i instead of NA + NAi. I could fix this.
  • Spline interpolation does only support meshgrid format, but not index pairs. Matlab supports that. There is already a note in _splinen_.m. I added a failing test using xtest.
  • Spline interpolation returns a row, when interpolating a single column, though with the correct values. I added a failing test using xtest.

I have not opened new bug reports nor checked for existing ones regarding the two spline interpolation issues.

Where to go from here?

  • I have not checked interp1.m, interp3.m, interpn.m. The cubic interpolation might also be Matlab incompatible.
  • Bilinear interpolation could also be implemented by convolution easily. This might be faster, maybe only for specific situations. When switching from interp2 to the convolutional approach in imresize.m to support anti-aliasing (submitted in bug #61760, not yet pushed), the speed-up was quite big for some tests.

Please let me know if the implementation needs improvements.

(file #52682, file #52683)

Christof Kaufmann <christofkaufmann>
Mon 03 Jan 2022 01:47:23 PM UTC, comment #1: 

Patches to fix compatibility are always welcome.

Markus Mützel <mmuetzel>
Project Member
Mon 03 Jan 2022 01:25:04 AM UTC, original submission:  

Using "cubic" for the method argument of interp2 is just an alias for "pchip" (piecewise cubic Hermite interpolating polynomial). However in Matlab a cubic convolution is used, as stated in their documentation of interp2. This method is also described on Wikipedia's Bicubic Interpolation page.

A tiny example to show the difference:

> interp2(magic(4), 1, 1.5, 'cubic')
ans = 8.1875

Matlab (R2021a):

> interp2(magic(4), 1, 1.5, 'cubic')
ans = 8.6250

The topic has already been discussed in bug #60322 (regarding imresize) and there is even a patch for imresize available to reach Matlab compatibility. Maybe this patch can be adapted for interp2 as well? What is your opinion on this?

Christof Kaufmann <christofkaufmann>


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #52707:  interp2_fix_bicubic3.diff added by christofkaufmann (14KiB - text/x-patch - bicubic interpolation - v3 (rebased))
file #52699:  interp2_fix_bicubic2.diff added by christofkaufmann (14KiB - text/x-patch - bicubic interpolation - v3)
file #52694:  interp2_fix_bicubic1.diff added by christofkaufmann (13KiB - text/x-patch - bicubic interpolation - v2)
file #52682:  interp2.m added by christofkaufmann (26KiB - text/x-objcsrc - bicubic interpolation - v1)
file #52683:  bicubic.pdf added by christofkaufmann (104KiB - application/pdf - bicubic interpolation - v1)


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by christofkaufmann (Submitted the item)

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.




    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2022-01-22 christofkaufmann Attached File- => Added interp2_fix_bicubic3.diff, #52707
    2022-01-20 christofkaufmann Attached File- => Added interp2_fix_bicubic2.diff, #52699
    2022-01-20 christofkaufmann Attached File- => Added interp2_fix_bicubic1.diff, #52694
    2022-01-19 christofkaufmann Attached File- => Added interp2.m, #52682
        Attached File- => Added bicubic.pdf, #52683

    Back to the top

    Powered by Savane 3.7