# bug #56170: [octave forge] (signal) results from fir1 differ from Matlab (inaccurate?)

 Submitter: John W. Eaton Submitted: Fri 19 Apr 2019 02:30:42 PM UTC Category: Octave Package Severity: 3 - Normal Priority: 5 - Normal Item Group: Inaccurate Result Status: Need Info Assigned to: None Originator Name: jwe Open/Closed:  * Open Release:  * dev Operating System:  * Any Fixed Release: None Planned Release: None
* Mandatory Fields

Add a New Comment Rich Markup

## Discussion

Sun 23 Feb 2020 09:36:37 PM UTC, comment #2:

I had a quick look into this topic.

Octave's fir1 () appears to be using fir2 () which looks like the filter response is derived by inverse DFT on a frequency response given on the frequency domain. But Matlabs fir1 () is based on a least squares linear phase filter design.

On the Matlab homepage we find: "fir1 uses a least-squares approximation to compute the filter coefficients and then smooths the impulse response with window"

So in the end two different concepts were compared.

I have searched the signal package for a solution and found a comparable function that was added after fir1 () was written. Octave's firls () should do the trick if the response is windowed by an appropriate window function. It follows a short "proof of concept" code. The plot shows:

i.) JWE's Matlab result as per attachment fir1-example.dat
ii.) JWE's Octave result as per attachment fir1-example.dat
iii.) firls () + hamming window for same cut off frequency

To run the code you have to download JWE's data file first. The result looks very promising. However to fix this somebody must overhaul fir1 ().

data = load ("fir1-example.dat");

resp = firls (2048, [0, 100/(48000/2), 100/(48000/2), 1], [1, 1, 0, 0]).';
win = hamming (2049).';
lowpass = resp.*win;
firls = lowpass / sum (lowpass);

hold on;
## fir1 (2048, 100/(48000/2), 'low') on Matlab
plot (data(:,1), 'g');
## fir1 (2048, 100/(48000/2), 'low') on Octave PKG Signal
plot (data(:,2), 'c');
plot (firls, 'm');
grid on;
legend({"Matlab","Octave PKG signal","Octave firls () CMD"});

Hg200 <hg200>
Fri 19 Apr 2019 03:41:45 PM UTC, comment #1:

Octave's fir1 is currently based on fir2. It converts its arguments into fir2 arguments and normalizes the result.

Maybe we should compare Octave's fir2 against Matlab for these examples and see if that is the underlying problem.

Or maybe the "FIXME: Consider using exact expression (in terms of sinc) for the impulse response rather than relying on fir2" needs to be looked at.

The equivalent calls to fir2 are

fir2 (2048, [0, 100/(48000/2), 100/(48000/2), 1], [1, 1, 0, 0], [], 2, [])
fir2 (2048, [0, 100/48000/2, 100/48000/2, 1], [1, 1, 0, 0], [], 2, [])

Mike Miller <mtmiller>
Fri 19 Apr 2019 02:30:42 PM UTC, original submission:

I received a report about differences between the results produced by Matlab and Octave's fir1 functions.  The example given was:

fir1 (2048, 100/(48000/2), 'low')

The result from Matlab is in column 1 of the attached file (fir1-example.dat) and the results I see for Octave are in column 2.  I see a maximum difference of almost 3%

In another case, the difference is larger.  I misinterpreted the original example and asked for confirmation on the maintainers mailing list for the following instead:

fir1 (2048, 100/48000/2, 'low')

The result from Matlab is in column 3 of the data file and the corresponding result from Octave is in column 4.  Here the maximum difference is about 5%

I'm using the development version of Octave and the signal package.  Octave hg id:  64461ccf3039  and signal package hg id:  2adde4d8b905.

John W. Eaton <jwe>

## Attached Files

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

Attach Files:

Comment:

Attached Files
file #46798:  fir1-example.dat added by jwe (124KiB - application/octet-stream)

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

## Mail Notification Carbon-Copy List

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

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only group members can vote.

## History

Follow 2 latest changes.

Date Changed by Updated Field Previous Value => Replaced by
2019-04-19 mtmiller StatusNone Need Info
2019-04-19 jwe Attached File- Added fir1-example.dat, #46798