File Exchange

image thumbnail


version (5.24 KB) by Carlos Adrian Vargas Aguilera
Low or high-pass Lanczos (cosine) signal filter.


Updated 04 May 2016

View License

Apply the Lanczos filter to a real time series, in the frequency space, i.e., using FFT which is faster than applied as a cosine filter in the time space.
Y = lanczosfilter(X,dT,cf,M,'low')
X - Time series
dT - Sampling interval (default 1)
Cf - Cut-off frequency (default half Nyquist)
M - Number of coefficients (default 100)
and 'low' or 'high' depending if you want to get the smooth or the noisy part of your data, respectively (default 'low', so it smooths).
NaN's elements are replaced by mean(X). If you have a better idea, just let me know.

It comes with an example, also take a look at the screenshot, where the filter is applied to each row.

Emery, W. J. and R. E. Thomson. "Data Analysis Methods in Physical Oceanography". Elsevier, 2d ed., 2004. Pages 533-539.

Cite As

Carlos Adrian Vargas Aguilera (2020). LanczosFilter.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Laís Fernandes

I am using your Lanczos filter on Matlab. I want use M = 211 but the code only permits me use [ ] for M. Look how I am doing:

[Y,coef,window,Cx,Ff] = lanczosfilter(preca(:,14),1,0.05,211,'low')

It is weird because even when I choose 100 for M, the function does not work.

Please help me!

Erik Koene

Excellent code!

MATLAB Answers


Is there a calculation for a desired M? Everytime I enter an M value it returns incorrect number of coefficients. This is what I am using:

t = 1:489; % x-axis scale
dt = 162; % period
Nf =1/(2*dt); % Nyquist Frequency
cf = Nf/2; % cut-off frequency
% Default: half Nyquist(sample_size/2)

n_coef = 100; % number of coefficients
pass = 'low';
%lanc_freq: frequency

[lanc_fdata,lanc_coef,lanc_window,lanc_fft,lanc_freq] = ...


can this code do bandpass filtering


Have you submitted the updated code?


Carlos - Thanks for the code! I am using it on my data (3792 points in time, dt=10-min, aiming to low pass 48-hr) and I find that as M increases, the frequency response improves and the filter is better (comparing it to a transform filter output)... If M =10 then it is bad, if M=2000 for e.g. then it is really good. The reference (Emery & Thompson) says that you get data loss from each end with increasing M, but this doesn't happen with me.. was hoping someone could help explain? Thanks!

huang xiaoyu


Francis Mingo

How to make a matlab code by Lanczos method? I really didn't know how...

Carlos Adrian Vargas Aguilera

Thank you Yanaki. In fact I have rewritten the code, but I haven't update it yet, because I'm reorginizing all of my programs. But thank you again, you are correct.

The author

Janaki Chandimala

the line
elseif ~(numel(varargin{3})==1) || ~isreal(varargin{3}) || (varargin{3}==round(varargin{3}))
should be corrected as
elseif ~(numel(varargin{3})==1) || ~isreal(varargin{3}) || ~(varargin{3}==round(varargin{3}))
to avoid M not being an integer. Otherwise the code works only for the default M .

Carlos Adrian Vargas Aguilera

Sorry, there's an error in the help. I wrote M greater is better, but it should read lower, and so, probably M=10 for default is better but this really depends on your problem.

We just have to remember that if M is high the window approximates to a boxcar (rectangle), but the Gibbs phenomenon becomes greater, which Lanczos tries to diminish!

In the screenshot, the x-axis are minutes, and there was aplied a 60-min low-pass filter to the figure at the top.

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!