Bordoglor's picture
Upload folder using huggingface_hub
302920f verified
# Copyright 2025-present the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Minimal wavelet implementation extracted from PyWavelets
This code contains portions derived from PyWavelets: Copyright (c) 2006-2012 Filip Wasilewski <http://en.ig.ma/>
Copyright (c) 2012- The PyWavelets Developers <https://github.com/PyWavelets/pywt>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original source: https://github.com/PyWavelets/pywt
"""
import math
from collections.abc import Sequence
class Wavelet:
"""
Minimal wavelet class that implements the most commonly used wavelets.
Supports:
- Daubechies wavelets: db1-db10, haar
- Symlets: sym2-sym10
- Coiflets: coif1-coif5
"""
def __init__(self, name: str):
"""
Initialize a wavelet by name.
Args:
name: Wavelet name (e.g., 'db4', 'haar', 'sym5', 'coif2')
"""
self.name = name.lower()
self._compute_filters()
def _compute_filters(self):
"""Compute the four filter banks from the base coefficients."""
if self.name == "haar":
# Haar is the same as db1
base_coeffs = _WAVELET_COEFFS["db1"]
elif self.name in _WAVELET_COEFFS:
base_coeffs = _WAVELET_COEFFS[self.name]
else:
raise ValueError(f"Unknown wavelet name '{self.name}'. Available wavelets: {list(_WAVELET_COEFFS.keys())}")
# Determine if this is a coiflet (needs sqrt(2) scaling)
scale_factor = math.sqrt(2) if self.name.startswith("coif") else 1.0
# Apply scaling to base coefficients
scaled_coeffs = [c * scale_factor for c in base_coeffs]
# Compute the four filter banks following PyWavelets convention
# rec_lo = scaled base coefficients
self._rec_lo = scaled_coeffs[:]
# dec_lo = rec_lo reversed
self._dec_lo = scaled_coeffs[::-1]
# rec_hi = alternating signs of dec_lo
self._rec_hi = [(-1) ** i * scaled_coeffs[len(scaled_coeffs) - 1 - i] for i in range(len(scaled_coeffs))]
# dec_hi = alternating signs of rec_lo
self._dec_hi = [(-1) ** (len(scaled_coeffs) - 1 - i) * scaled_coeffs[i] for i in range(len(scaled_coeffs))]
@property
def dec_lo(self) -> Sequence[float]:
"""Lowpass decomposition filter."""
return self._dec_lo
@property
def dec_hi(self) -> Sequence[float]:
"""Highpass decomposition filter."""
return self._dec_hi
@property
def rec_lo(self) -> Sequence[float]:
"""Lowpass reconstruction filter."""
return self._rec_lo
@property
def rec_hi(self) -> Sequence[float]:
"""Highpass reconstruction filter."""
return self._rec_hi
@property
def dec_len(self) -> int:
"""Decomposition filters length."""
return len(self._dec_lo)
@property
def rec_len(self) -> int:
"""Reconstruction filters length."""
return len(self._rec_lo)
@property
def filter_bank(self) -> tuple[Sequence[float], Sequence[float], Sequence[float], Sequence[float]]:
"""Tuple of all four filter banks (dec_lo, dec_hi, rec_lo, rec_hi)."""
return (self.dec_lo, self.dec_hi, self.rec_lo, self.rec_hi)
def __len__(self) -> int:
"""Return the length of the decomposition filters."""
return self.dec_len
def __repr__(self) -> str:
return f"Wavelet(name='{self.name}')"
# Wavelet coefficients extracted from PyWavelets
# These are the reconstruction lowpass filter coefficients
_WAVELET_COEFFS = {
# Daubechies wavelets
"db1": [
0.7071067811865475244008443621048490392848359376884740365883398,
0.7071067811865475244008443621048490392848359376884740365883398,
],
"db2": [
0.4829629131445341433748715998644486838169524195042022752011715,
0.8365163037378079055752937809168732034593703883484392934953414,
0.2241438680420133810259727622404003554678835181842717613871683,
-0.1294095225512603811744494188120241641745344506599652569070016,
],
"db3": [
0.3326705529500826159985115891390056300129233992450683597084705,
0.80689150931333875,
0.45987750211933132,
-0.13501102001039084,
-0.085441273882241486,
0.035226291882100656,
],
"db4": [
0.2303778133088965008632911830440708500016152482483092977910968,
0.7148465705529156470899219552739926037076084010993081758450110,
0.6308807679298589078817163383006152202032229226771951174057473,
-0.02798376941685985421141374718007538541198732022449175284003358,
-0.1870348117190930840795706727890814195845441743745800912057770,
0.03084138183556076362721936253495905017031482172003403341821219,
0.03288301166688519973540751354924438866454194113754971259727278,
-0.01059740178506903210488320852402722918109996490637641983484974,
],
"db5": [
0.1601023979741929144807237480204207336505441246250578327725699,
0.6038292697971896705401193065250621075074221631016986987969283,
0.7243085284377729277280712441022186407687562182320073725767335,
0.1384281459013207315053971463390246973141057911739561022694652,
-0.2422948870663820318625713794746163619914908080626185983913726,
-0.03224486958463837464847975506213492831356498416379847225434268,
0.07757149384004571352313048938860181980623099452012527983210146,
-0.006241490212798274274190519112920192970763557165687607323417435,
-0.01258075199908199946850973993177579294920459162609785020169232,
0.003335725285473771277998183415817355747636524742305315099706428,
],
"db6": [
0.1115407433501094636213239172409234390425395919844216759082360,
0.4946238903984530856772041768778555886377863828962743623531834,
0.7511339080210953506789344984397316855802547833382612009730420,
0.3152503517091976290859896548109263966495199235172945244404163,
-0.2262646939654398200763145006609034656705401539728969940143487,
-0.1297668675672619355622896058765854608452337492235814701599310,
0.09750160558732304910234355253812534233983074749525514279893193,
0.02752286553030572862554083950419321365738758783043454321494202,
-0.03158203931748602956507908069984866905747953237314842337511464,
0.0005538422011614961392519183980465012206110262773864964295476524,
0.004777257510945510639635975246820707050230501216581434297593254,
-0.001077301085308479564852621609587200035235233609334419689818580,
],
"db7": [
0.07785205408500917901996352195789374837918305292795568438702937,
0.3965393194819173065390003909368428563587151149333287401110499,
0.7291320908462351199169430703392820517179660611901363782697715,
0.4697822874051931224715911609744517386817913056787359532392529,
-0.1439060039285649754050683622130460017952735705499084834401753,
-0.2240361849938749826381404202332509644757830896773246552665095,
0.07130921926683026475087657050112904822711327451412314659575113,
0.08061260915108307191292248035938190585823820965629489058139218,
-0.03802993693501441357959206160185803585446196938467869898283122,
-0.01657454163066688065410767489170265479204504394820713705239272,
0.01255099855609984061298988603418777957289474046048710038411818,
0.0004295779729213665211321291228197322228235350396942409742946366,
-0.001801640704047490915268262912739550962585651469641090625323864,
0.0003537137999745202484462958363064254310959060059520040012524275,
],
"db8": [
0.05441584224310400995500940520299935503599554294733050397729280,
0.3128715909142999706591623755057177219497319740370229185698712,
0.6756307362972898068078007670471831499869115906336364227766759,
0.5853546836542067127712655200450981944303266678053369055707175,
-0.01582910525634930566738054787646630415774471154502826559735335,
-0.2840155429615469265162031323741647324684350124871451793599204,
0.0004724845739132827703605900098258949861948011288770074644084096,
0.1287474266204784588570292875097083843022601575556488795577000,
-0.01736930100180754616961614886809598311413086529488394316977315,
-0.04408825393079475150676372323896350189751839190110996472750391,
0.01398102791739828164872293057263345144239559532934347169146368,
0.008746094047405776716382743246475640180402147081140676742686747,
-0.004870352993451574310422181557109824016634978512157003764736208,
-0.0003917403733769470462980803573237762675229350073890493724492694,
0.0006754494064505693663695475738792991218489630013558432103617077,
-0.0001174767841247695337306282316988909444086693950311503927620013,
],
"db9": [
0.03807794736387834658869765887955118448771714496278417476647192,
0.2438346746125903537320415816492844155263611085609231361429088,
0.6048231236901111119030768674342361708959562711896117565333713,
0.6572880780513005380782126390451732140305858669245918854436034,
0.1331973858250075761909549458997955536921780768433661136154346,
-0.2932737832791749088064031952421987310438961628589906825725112,
-0.09684078322297646051350813353769660224825458104599099679471267,
0.1485407493381063801350727175060423024791258577280603060771649,
0.03072568147933337921231740072037882714105805024670744781503060,
-0.06763282906132997367564227482971901592578790871353739900748331,
0.0002509471148314519575871897499885543315176271993709633321834164,
0.02236166212367909720537378270269095241855646688308853754721816,
-0.004723204757751397277925707848242465405729514912627938018758526,
-0.004281503682463429834496795002314531876481181811463288374860455,
0.001847646883056226476619129491125677051121081359600318160732515,
0.0002303857635231959672052163928245421692940662052463711972260006,
-0.0002519631889427101369749886842878606607282181543478028214134265,
0.00003934732031627159948068988306589150707782477055517013507359938,
],
"db10": [
0.02667005790055555358661744877130858277192498290851289932779975,
0.1881768000776914890208929736790939942702546758640393484348595,
0.5272011889317255864817448279595081924981402680840223445318549,
0.6884590394536035657418717825492358539771364042407339537279681,
0.2811723436605774607487269984455892876243888859026150413831543,
-0.2498464243273153794161018979207791000564669737132073715013121,
-0.1959462743773770435042992543190981318766776476382778474396781,
0.1273693403357932600826772332014009770786177480422245995563097,
0.09305736460357235116035228983545273226942917998946925868063974,
-0.07139414716639708714533609307605064767292611983702150917523756,
-0.02945753682187581285828323760141839199388200516064948779769654,
0.03321267405934100173976365318215912897978337413267096043323351,
0.003606553566956169655423291417133403299517350518618994762730612,
-0.01073317548333057504431811410651364448111548781143923213370333,
0.001395351747052901165789318447957707567660542855688552426721117,
0.001992405295185056117158742242640643211762555365514105280067936,
-0.0006858566949597116265613709819265714196625043336786920516211903,
-0.0001164668551292854509514809710258991891527461854347597362819235,
0.00009358867032006959133405013034222854399688456215297276443521873,
-0.00001326420289452124481243667531226683305749240960605829756400674,
],
# Symlets
"sym2": [0.48296291314469025, 0.83651630373746899, 0.22414386804185735, -0.12940952255092145],
"sym3": [
0.33267055295095688,
0.80689150931333875,
0.45987750211933132,
-0.13501102001039084,
-0.085441273882241486,
0.035226291882100656,
],
"sym4": [
0.032223100604042702,
-0.012603967262037833,
-0.099219543576847216,
0.29785779560527736,
0.80373875180591614,
0.49761866763201545,
-0.02963552764599851,
-0.075765714789273325,
],
"sym5": [
0.019538882735286728,
-0.021101834024758855,
-0.17532808990845047,
0.016602105764522319,
0.63397896345821192,
0.72340769040242059,
0.1993975339773936,
-0.039134249302383094,
0.029519490925774643,
0.027333068345077982,
],
"sym6": [
-0.007800708325034148,
0.0017677118642428036,
0.044724901770665779,
-0.021060292512300564,
-0.072637522786462516,
0.3379294217276218,
0.787641141030194,
0.49105594192674662,
-0.048311742585632998,
-0.11799011114819057,
0.0034907120842174702,
0.015404109327027373,
],
"sym7": [
0.010268176708511255,
0.0040102448715336634,
-0.10780823770381774,
-0.14004724044296152,
0.28862963175151463,
0.76776431700316405,
0.5361019170917628,
0.017441255086855827,
-0.049552834937127255,
0.067892693501372697,
0.03051551316596357,
-0.01263630340325193,
-0.0010473848886829163,
0.0026818145682578781,
],
"sym8": [
0.0018899503327594609,
-0.0003029205147213668,
-0.014952258337048231,
0.0038087520138906151,
0.049137179673607506,
-0.027219029917056003,
-0.051945838107709037,
0.3644418948353314,
0.77718575170052351,
0.48135965125837221,
-0.061273359067658524,
-0.14329423835080971,
0.0076074873249176054,
0.031695087811492981,
-0.00054213233179114812,
-0.0033824159510061256,
],
"sym9": [
0.0010694900329086053,
-0.00047315449868008311,
-0.010264064027633142,
0.0088592674934004842,
0.06207778930288603,
-0.018233770779395985,
-0.19155083129728512,
0.035272488035271894,
0.61733844914093583,
0.717897082764412,
0.238760914607303,
-0.054568958430834071,
0.00058346274612580684,
0.03022487885827568,
-0.01152821020767923,
-0.013271967781817119,
0.00061978088898558676,
0.0014009155259146807,
],
"sym10": [
-0.00045932942100465878,
0.000057036083618494284,
0.0045931735853118284,
-0.00080435893201654491,
-0.02035493981231129,
0.0057649120335819086,
0.049994972077376687,
-0.0319900568824278,
-0.035536740473817552,
0.38382676106708546,
0.7695100370211071,
0.47169066693843925,
-0.070880535783243853,
-0.15949427888491757,
0.011609893903711381,
0.045927239231092203,
-0.0014653825813050513,
-0.0086412992770224222,
0.000095632670722894754,
0.00077015980911449011,
],
# Coiflets (note: these will be multiplied by sqrt(2) in the class)
"coif1": [
-0.05142972847076845595317549230122688830344559947132656813651045,
0.2389297284707684559531754923012268883034455994713265681365104,
0.6028594569415369119063509846024537766068911989426531362730209,
0.2721405430584630880936490153975462233931088010573468637269790,
-0.05142972847076845595317549230122688830344559947132656813651045,
-0.01107027152923154404682450769877311169655440052867343186348954,
],
"coif2": [
0.01158759673871686817889714882853120395708315073355502818875931,
-0.02932013798346856448679594524397843054053420947418409889774786,
-0.04763959031100813225872995081511549408622753909592460525840745,
0.2730210465347666137982239328923516270034828327990699588033501,
0.5746823938568638472459483149751499367740786490481481391460366,
0.2948671936956191896750637208703777973914107635455611537640778,
-0.05408560709171142997443672832006888537570221990444706777525838,
-0.04202648046077160694657530752545884878978719268926222513485613,
0.01674441016327950635146257083249391698866289538037299820224006,
0.003967883612962012109043447090269950094081810916481648252817197,
-0.001289203356140659543141355500990678257894936161704492503370186,
-0.0005095053991076441489598480835620951586540050976664367876412655,
],
"coif3": [
-0.002682418670922068664584689955153722375535836177157637134187840,
0.005503126707831385107969640263617469178794666057252906037981936,
0.01658356047917034608134280439996549525220639437145367606178002,
-0.04650776447872697640390293095170192691113917841041002855534619,
-0.04322076356021191118175840907244577856782537221435748296465882,
0.2865033352736474630249006862976158896891076238443844211133873,
0.5612852568703300445990941995240077241406247774064453800050914,
0.3029835717728241602862575774374668529867757043461413348549577,
-0.05077014075488886159516471867138370972545857441670871832472707,
-0.05819625076158553022607041679522801089624825903982541419721721,
0.02443409432116695639462954438418928805487699080947974989338820,
0.01122924096203786563399489540091488781245346096838814728167341,
-0.006369601011048822977293753932627342482077585617391852852955559,
-0.001820458915566242322836631665832145136570132777862391313328351,
0.0007902051009575939937150950543290226440287715441826917281929124,
0.0003296651737931830308416338897758022998655744276957481989605186,
-0.00005019277455327664998007173088097694083956570594580641192332170,
-0.00002446573425530813115445387662881902303945941576472342106918209,
],
"coif4": [
0.0006309612114309468490753696608619526520153127603444406835368201,
-0.001152225143769973488683007937016166047881572156705066038094891,
-0.005194525163470323267558201363327294331811309729430512113592118,
0.011360930899781950641704454327495718441159520023894304805142070,
0.018945061045616642675204041814669158097013442370604397885045773,
-0.051719843705815280952009072709014825996085808127950893370164031,
-0.034486140470944806827159094088779177962124655341862998060866093,
0.30227251053656843537076103037201073987915654650542997843779746,
0.55454790624088107896085831311334062609863843227892842936901802,
0.30791766802517503548651698686002846493302655084140026096325632,
-0.04352500928126570063143077306027663648139777048547894956715080,
-0.06488795097143100103160862688937301504802507374726020928892066,
0.01988077364815951966984001670075537628468542316950829728327598,
0.01763894787126169746077061344050946967036166456074020965866088,
-0.007366054847173363935072651649653007115003169492027095040477055,
-0.002312432307658842282830374733100847689924654369899030169556169,
0.0014260063442333715226509754100697734398974715092509045804651032,
0.0004666984635537353670445650012678936080062341977092967649055398,
-0.0001270007842387334077388950072420113055088253899932456267893098,
-0.0001130536369789104919020013936507623832962772709844179610938550,
0.00003048364879677801030096883509693508426509710688913073244616617,
0.00001266744808933008194725929652978169473830765616675686100903086,
-0.000001584926580756893754069651095690142946796090146306140001598,
-0.000001123948088281542889088159169056968300680087779667334879506,
],
"coif5": [
-0.0001444992186438190986841213894961515720877049723502928655308158,
0.0002541649492011946935899015644804259825374993423205648946709984,
0.0015016192805175522217354963668928299350735326077949346507003370,
-0.0029411108712655515426850089360913424188662278991737055486839309,
-0.0071777671514877191801104649507158618871157411936681659380839993,
0.016680426640070654149267486742006854522334094142598667043628439,
0.019433238433489604119639447772308536988043628308900006988094899,
-0.064934946567212502582522008002547701764467194128935170823607736,
-0.036249793089132571825087765037251085892962369926089901862924065,
0.29804266217809436069693444260411251439893892734398765007426945,
0.55749162970920071628061190166750547398568080072951806509736879,
0.30731644529206781686031633026138686170779831068030092889493625,
-0.047088034719761145117688715152051398948700623993077406913889346,
-0.068890522508050074805015336128652797797076949077503388892816063,
0.020697343297747766068568936830651656003659188170019885439659031,
0.021640668655956855043817421090949779825140639715020046717736369,
-0.0081089373078953680936950024508066654697766705721301481097854397,
-0.0049881737671041853808073796089816945023009226058734090095808033,
0.0024486914321021269742893936892468103370072825113159100554056433,
0.0014095103899593442621166984842002926701899968616244946547893994,
-0.0005637801876093825733169550088901318936072015721509885859509815,
-0.0002859004477225750899655442618734663056802618537327806113618985,
0.00012739637513815208006169426577159456832051015616166327985688948,
0.00005416263410701044073894700796327336007788688985721449717765655,
-0.00001736867944346280636144226913926838103159698473080996002509476,
-0.00001392656190060010871169838885327726938969652863554900825705905,
0.000003582065515946048838215026334503092089635988710863959063568069,
0.000001914022895847318655772885654240700542388103264097264264779554,
-0.00000031262488377016899432194683906058825900951892071223097080609,
-0.00000034030635502511647536690616071863203084936306302829968850306,
0.000000059816065238516936893488966688516710847096926983547983503726,
0.000000047001427849456491830476615736016736014244615701046223529866,
-0.000000006158615709678364180659098549671046676203853020063205641804,
-0.000000009225635096344935080070901936862847863830913641424076095562,
0.000000001028486074518821265015830073593127726988903862842106883701,
0.000000001168734175186263778695686067593866982925127816327529890618,
-0.00000000009468626176069127302554946536142654377756003084491946024,
-0.00000000016230233142152041788509334089966065953985768924968863072,
0.000000000015076656859346950325398899897135970089618140503825462985,
0.000000000015770990416421915106306877550025550097686639869166742016,
-0.000000000001084900468648598127623517893686893316653633996513097476,
-0.000000000001968659779411804788815966829825641065085077654946686012,
0.000000000000098745634639726633264577838416327095717894829823436076,
0.000000000000196734781460508097097473336847436635654948853090962606,
-0.000000000000008021080145299890797556481653726965016924825037889883,
-0.000000000000021030408801651651406095853493993966926736862877194669,
0.000000000000000723888697830915633925166893301949334507697669655816,
0.000000000000001943208515072761516084547140065815027641765976721267,
],
}
def wavelist() -> list[str]:
"""Return a list of available wavelet names."""
return list(_WAVELET_COEFFS.keys()) + ["haar"]