Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'scipy': 0.05; 'linear': 0.07; 'problem?': 0.07; 'python': 0.09; "'''": 0.09; 'indeed,': 0.09; 'library...': 0.09; 'rows': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; ':-)': 0.13; 'alpha': 0.15; 'stack': 0.15; 'arrays.': 0.16; 'cc:name:python list': 0.16; 'combinations': 0.16; 'dimension': 0.16; 'formula': 0.16; 'frames': 0.16; 'loops': 0.16; 'numpy': 0.16; 'quadratic': 0.16; 'y1,': 0.16; 'exists': 0.17; 'appropriate': 0.20; 'code.': 0.20; 'sort': 0.21; 'supposed': 0.21; 'explicit': 0.22; 'raise': 0.24; 'somewhere': 0.24; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'compiled': 0.27; 'subject:/': 0.28; 'arrays': 0.29; 'routine': 0.29; 'vector': 0.29; 'array': 0.29; 'probably': 0.29; "i'm": 0.29; 'maybe': 0.29; 'e.g.': 0.30; 'code': 0.31; 'anybody': 0.32; 'could': 0.32; 'problem': 0.33; '(with': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'thanks': 0.34; 'doing': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'should': 0.36; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'page': 0.38; 'your': 0.60; 'easy': 0.60; 'from:no real name:2**0': 0.60; 'skip:u 10': 0.60; 'between': 0.63; 'ever': 0.63; 'more': 0.63; 'here': 0.65; 'oscar': 0.84; 'do:': 0.91; 'faster.': 0.91 Newsgroups: comp.lang.python Date: Fri, 11 Jan 2013 04:20:10 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=193.30.192.187; posting-account=y3pxCwoAAAAPbArC5DgIDd7YJOIoKOy_ References: <5b5b7e09-7482-4609-b774-d143de366615@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 193.30.192.187 MIME-Version: 1.0 Subject: Re: Interpolating/crossfading a stack of matrices From: raphael@mameghani.de To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: raphael@mameghani.de, Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357906820 news.xs4all.nl 6851 [2001:888:2000:d::a6]:47451 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36618 >> Hi, >> >> I want to interpolate (with quadratic splines) a stack of 2D-arrays/matrices >> y1, y2, y3, ... in a third dimension (which I call x) e.g. for crossfading >> images. I already have a working code which unfortunately still contains two >> explicit loops over the rows and colums of the matrices. Inside these loops I >> simply use 'interp1d' from scipy suitable for 1D-interpolations. Is anybody >> here aware of a better, more efficient solution of my problem? Maybe >> somewhere out there a compiled routine for my problem already exists in a >> python library... :-) > Since numpy arrays make it so easy to form linear combinations of > arrays without loops I would probably eliminate the loops and just > form the appropriate combinations of the image arrays. For example, to > use linear interpolation you could do: > > > > def interp_frames_linear(times, frames, t): > > '''times is a vector of floats > > frames is a 3D array whose nth page is the image for time t[n] > > t is the time to interpolate for > > ''' > > # Find the two frames to interpolate between > > # Probably a better way of doing this > > for n in range(len(t)-1): > > if times[n] <= t < times[n+1]: > > break > > else: > > raise OutOfBoundsError > > > > # Interpolate between the two images > > alpha = (t - times[n]) / (times[n+1] - times[n]) > > return (1 - alpha) * frames[:, :, n] + alpha * frames[:, :, n+1] > > > > I'm not really sure how quadratic interpolation is supposed to work > (I've only ever used linear and cubic) but you should be able to do > the same sort of thing. > > Oscar Indeed, the 'manual' reimplementation of the interpolation formula using numpy arrays significantly sped up the code. The numexpr package made it even faster. Thanks a lot for your advice! Raphael