├── .gitignore ├── LICENSE ├── README.md ├── conda ├── README └── ldbp_env.yml └── ldbp ├── .gitignore ├── config ├── essfm.ini ├── isit.ini └── isit_pruning.ini ├── jobscript_essfm ├── jobscript_isit ├── kill_last_pid ├── ldbp.py └── lib └── fir.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LDBP: Learned Digital Backpropagation 2 | 3 | ## Getting Started 4 | 5 | The code is based on [TensorFlow](https://www.tensorflow.org/) 1.13.1 and may 6 | not work properly with other (older or newer) versions. It is recommended to create a 7 | dedicated conda environment using the YAML file in the folder `conda` as 8 | follows: 9 | 10 | ```console 11 | (base)~$ conda env create -f ldbp_env.yml 12 | (base)~$ conda activate ldbp_env 13 | ``` 14 | 15 | Afterwards, it should be possible to run the provided jobscripts in the folder `ldbp`. For example: 16 | 17 | ```console 18 | (ldbp_env)~$ ./jobscript_isit 19 | ``` 20 | 21 | To train for different scenarios, most of the parameters and training options are set in a configuration file located in the folder `config`. 22 | 23 | 40 | 41 | 45 | 46 | ## Additional Information 47 | 48 | This repository is based on joint work with [Henry D. 49 | Pfister](http://pfister.ee.duke.edu). 50 | If you decide to use the source code for your research, please make sure 51 | to cite our paper(s): 52 | 53 | * C. Häger and H. D. Pfister, "[Physics-Based Deep Learning for Fiber-Optic Communication Systems](https://arxiv.org/abs/2010.14258)", in *IEEE J. Sel. Areas Commun.* (to appear), 2020 54 | 55 | * C. Häger and H. D. Pfister, "[Nonlinear Interference Mitigation via Deep Neural Networks](https://arxiv.org/abs/1710.06234)", in Proc. *Optical Fiber Communication Conf. (OFC)*, San Diego, CA, March 2018 56 | 57 | * C. Häger and H. D. Pfister, "[Deep Learning of the Nonlinear Schrödinger Equation in Fiber-Optic Communication](https://arxiv.org/abs/1804.02799)", In Proc. *IEEE Int. Symp. on Information Theory (ISIT)*, Vail, CO, June 2018 58 | 59 | -------------------------------------------------------------------------------- /conda/README: -------------------------------------------------------------------------------- 1 | # to create environment: 2 | conda env create -f ldbp_env.yml 3 | 4 | # to remove 5 | conda env remove -n ldbp_env 6 | -------------------------------------------------------------------------------- /conda/ldbp_env.yml: -------------------------------------------------------------------------------- 1 | name: ldbp_env 2 | channels: 3 | - conda-forge 4 | dependencies: 5 | - python==3.7.3 6 | - pip 7 | - numpy==1.16.4 8 | - scipy==1.2.1 9 | - tensorflow==1.13.1 10 | - pip: 11 | - tqdm 12 | - configargparse 13 | -------------------------------------------------------------------------------- /ldbp/.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | log 3 | last_pid 4 | -------------------------------------------------------------------------------- /ldbp/config/essfm.ini: -------------------------------------------------------------------------------- 1 | [system parameters] 2 | span length [km] = 80 3 | alpha [dB/km] = 0.2 4 | D [ps/nm/km] = 17 5 | gamma [1/W/km] = 1.3 6 | amplifier noise figure [dB] = 5.0 7 | sigma scaling = 2 8 | number of spans = 25 9 | symbol rate [Gbaud] = 10.7 10 | modulation = Gaussian 11 | RRC roll-off = 0.1 12 | RRC delay = 10 13 | data symbols per block = 256 14 | low-pass filter bandwidth [GHz] = 21.4 15 | analog oversampling = 6 16 | digital oversampling = 2 17 | 18 | [LDBP parameters] 19 | step size method = linear 20 | split step method = asymmetric 21 | less steps than spans = yes 22 | total steps = 4 23 | cd filter method = LS-CO 24 | cd filter bandwidth = 0.6 25 | cd filter max out-of-band gain = 0.8 26 | optimize cd filters = no 27 | optimize Kerr parameters = yes 28 | tied Kerr parameters = yes 29 | nl filter length = 41 30 | 31 | [training] 32 | minibatch size = 100 33 | optimizer = adam 34 | summary writing interval = 100 35 | save results to file = yes 36 | 37 | [data generation] 38 | forward steps per span = 50 39 | number of queue elements = 50000 40 | generation batch size = 10 41 | number of parallel processors = 10 42 | data replication factor = 150 43 | minimum elements after dequeue = 2000 44 | -------------------------------------------------------------------------------- /ldbp/config/isit.ini: -------------------------------------------------------------------------------- 1 | [system parameters] 2 | span length [km] = 80 3 | alpha [dB/km] = 0.2 4 | D [ps/nm/km] = 17 5 | gamma [1/W/km] = 1.3 6 | amplifier noise figure [dB] = 5.0 7 | sigma scaling = 2 8 | number of spans = 25 9 | symbol rate [Gbaud] = 10.7 10 | modulation = Gaussian 11 | RRC roll-off = 0.1 12 | RRC delay = 10 13 | data symbols per block = 256 14 | low-pass filter bandwidth [GHz] = 21.4 15 | analog oversampling = 6 16 | digital oversampling = 2 17 | 18 | [LDBP parameters] 19 | step size method = linear 20 | split step method = asymmetric 21 | steps per span = 1 22 | cd filter method = LS-CO 23 | cd filter length = 9 24 | cd filter bandwidth = 0.6 25 | cd filter max out-of-band gain = 1.05 26 | optimize cd filters = yes 27 | #pruning = yes 28 | #target cd filter length = -4,3 29 | 30 | [training] 31 | minibatch size = 100 32 | optimizer = adam 33 | summary writing interval = 100 34 | save results to file = yes 35 | 36 | [data generation] 37 | forward steps per span = 50 38 | number of queue elements = 50000 39 | generation batch size = 10 40 | number of parallel processors = 10 41 | data replication factor = 150 42 | minimum elements after dequeue = 2000 43 | -------------------------------------------------------------------------------- /ldbp/config/isit_pruning.ini: -------------------------------------------------------------------------------- 1 | [system parameters] 2 | span length [km] = 80 3 | alpha [dB/km] = 0.2 4 | D [ps/nm/km] = 17 5 | gamma [1/W/km] = 1.3 6 | amplifier noise figure [dB] = 5.0 7 | sigma scaling = 2 8 | number of spans = 25 9 | symbol rate [Gbaud] = 10.7 10 | modulation = Gaussian 11 | RRC roll-off = 0.1 12 | RRC delay = 10 13 | data symbols per block = 256 14 | low-pass filter bandwidth [GHz] = 21.4 15 | analog oversampling = 6 16 | digital oversampling = 2 17 | 18 | [LDBP parameters] 19 | step size method = linear 20 | split step method = asymmetric 21 | steps per span = 1 22 | cd filter method = LS-CO 23 | cd filter length = 9 24 | cd filter bandwidth = 0.6 25 | cd filter max out-of-band gain = 1.05 26 | optimize cd filters = yes 27 | pruning = yes 28 | target cd filter length = 5,3 29 | 30 | [training] 31 | minibatch size = 100 32 | optimizer = adam 33 | summary writing interval = 100 34 | save results to file = yes 35 | 36 | [data generation] 37 | forward steps per span = 50 38 | number of queue elements = 50000 39 | generation batch size = 10 40 | number of parallel processors = 10 41 | data replication factor = 150 42 | minimum elements after dequeue = 2000 43 | -------------------------------------------------------------------------------- /ldbp/jobscript_essfm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo process ID: $$ 3 | echo $$ > last_pid 4 | 5 | # starts at SNR = 5.2 dB 6 | # ends at SNR = 18.2 dB 7 | python3 ldbp.py [-5,-4,-3,-2] 0.001 5000 --config_path=config/essfm.ini 8 | -------------------------------------------------------------------------------- /ldbp/jobscript_isit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo process ID: $$ 3 | echo $$ > last_pid 4 | 5 | # starts at SNR = 20.9 dB 6 | # converges to 22.5 dB (maximum) 7 | python3 ldbp.py [0] 0.001 1500 --config_path=config/isit.ini 8 | 9 | # 150,000 iterations = approx. 2 hours 10 | #python3 ldbp.py [-2,-1,0,1] 0.001 150000 --config_path=config/isit_pruning.ini 11 | -------------------------------------------------------------------------------- /ldbp/kill_last_pid: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | kill -9 -`cat last_pid` 3 | -------------------------------------------------------------------------------- /ldbp/ldbp.py: -------------------------------------------------------------------------------- 1 | #========================================================# 2 | # Learned Digital Backpropagation (LDBP) 3 | # Author: Christian Haeger (christian.haeger@chalmers.se) 4 | # Last modified: December, 2018 5 | #========================================================# 6 | # imports and constants {{{ 7 | #========================================================# 8 | import tensorflow as tf 9 | import sys # sys.exit() 10 | import warnings 11 | import os # os.path.exists(), os.environ['v'], os.makedirs() 12 | import numpy as np 13 | import scipy as sp # sp.fft(), sp.linalg.solve(), sp.optimize.fsolve(), sp.special.erf() 14 | import time # time.gmtime(), time.strftime() 15 | import math # math.isnan() 16 | import random # random.shuffle() 17 | import threading # threading.Thread() 18 | import multiprocessing 19 | import argparse as ap 20 | import configparser 21 | import shutil # shutil.copyfile(src, dst) 22 | from lib import fir # fir.cd_fir_filter() 23 | 24 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # avoid TF logging output for GPU devices etc. 25 | sp.set_printoptions(precision = 4, suppress = True) 26 | np.set_printoptions(precision = 4) 27 | #np.seterr(divide='ignore', invalid='ignore') # ignore "divide by 0" warning 28 | 29 | # constants 30 | co_h = 6.6260657e-34 31 | co_c0 = 299792458 32 | co_lambda = 1550.0e-9 33 | co_dB = 10.0*np.log10(np.exp(1.0)) 34 | nu = co_c0/co_lambda 35 | dB_conv = 4.342944819032518 36 | 37 | # }}} 38 | #========================================================# 39 | # functions {{{ 40 | #========================================================# 41 | def rrcosine(rolloff, delay, OS): 42 | """ Root-raised cosine filter for pulse shaping 43 | Args: 44 | rolloff: between 0 and 1 45 | delay: in symbols 46 | OS: oversampling factor (samples per symbol) 47 | 48 | Returns: 49 | A vector of length 2*(OS*delay)+1 50 | """ 51 | rrcos = np.zeros(2*delay*OS+1) 52 | rrcos[delay*OS] = 1 + rolloff*(4/np.pi-1) 53 | for i in range(1,delay*OS+1): 54 | t = i/OS 55 | if(t == 1/4/rolloff): 56 | val = rolloff/np.sqrt(2)*((1+2/np.pi)*np.sin(np.pi/(4*rolloff)) + (1-2/np.pi)*np.cos(np.pi/(4*rolloff))) 57 | else: 58 | val = (np.sin(np.pi*t*(1-rolloff)) + 4*rolloff*t*np.cos(np.pi*t*(1+rolloff))) / (np.pi*t*(1-(4*rolloff*t)**2)) 59 | rrcos[delay*OS+i] = val 60 | rrcos[delay*OS-i] = val 61 | return rrcos / np.sqrt(np.sum(rrcos**2)) 62 | 63 | def get_fvec(N,fs): 64 | return np.concatenate((np.linspace(0,N//2-1,N//2), np.linspace(-N//2,-1,N//2))) * fs/N 65 | 66 | def tf_print(tmp_var): 67 | init = tf.global_variables_initializer() 68 | sess = tf.Session() 69 | sess.run(init) 70 | print(sess.run(tmp_var)) 71 | 72 | def get_optimizer(): 73 | if optimizer == "adam": 74 | opt = tf.train.AdamOptimizer(learning_rate, float(conf_train['adam_A']), float(conf_train['adam_B'])) 75 | elif optimizer == "rmsprop": 76 | opt = tf.train.RMSPropOptimizer(learning_rate, float(conf_train['rmsprop_A']), float(conf_train['rmsprop_B'])) 77 | elif optimizer == "adadelta": 78 | opt = tf.train.AdadeltaOptimizer(learning_rate, float(conf_train['adadelta_A'])) 79 | elif optimizer == "adagrad": 80 | opt = tf.train.AdagradOptimizer(learning_rate, float(conf_train['adagrad_A'])) 81 | else: 82 | raise ValueError("wrong optimizer string: optimizer = '"+optimizer+"'") 83 | return opt 84 | 85 | def complex_multiply(x,y): 86 | """ 87 | Args: 88 | x: Tensor of shape = [batch_size, N, 2] 89 | y: Tensor of shape = [batch_size, N, 2] 90 | 91 | Returns: 92 | A Tensor of shape = [batch_size, N, 2] 93 | """ 94 | xr = x[:,:,0] 95 | xi = x[:,:,1] 96 | yr = y[:,:,0] 97 | yi = y[:,:,1] 98 | return tf.stack([xr*yr-xi*yi, xr*yi+xi*yr], axis=2) 99 | 100 | def tf_real_filter(init_coeffs, opt=False): 101 | """ Create arbitrary FIR filter with real coefficients 102 | 103 | Args: 104 | init_coeffs: real numpy array of shape [filter_length,] with initial filter coefficients 105 | opt: (Optional) True for variable, False for constant, default=False 106 | 107 | Returns: 108 | A Tensor with shape = [filter_length] 109 | """ 110 | 111 | if opt == True: 112 | h = tf.Variable(init_coeffs, dtype=tf.float32) 113 | else: 114 | h = tf.constant(init_coeffs, tf.float32) 115 | 116 | return h 117 | 118 | def tf_real_symmetric_filter(init_coeffs, opt=False, mask=1): 119 | """ Create odd-length symmetric FIR filter with real coefficients 120 | 121 | For symmetric filters of length 2*L+1, there are (L+1) tunable parameters: 122 | coeffs: h_L, ..., h_1, [h_0, h_1, ..., h_L] 123 | 124 | Args: 125 | init_coeffs: real numpy array of shape [filter_length,] with initial filter coefficients, 126 | filter_length should be odd 127 | coefficients should be symmetric 128 | opt: (Optional) True for variable, False for constant, default=False 129 | mask: (Optional) binary mask for pruning, default=1 130 | 131 | Returns: 132 | A Tensor with shape = [filter_length] 133 | """ 134 | 135 | filter_length = len(init_coeffs) 136 | if filter_length%2 is 0: 137 | raise ValueError("filter length has to be odd: filter_length = {}".format(filter_length)) 138 | filter_delay = (filter_length-1)//2 139 | right_half = init_coeffs[filter_delay::] 140 | 141 | for i in range(filter_delay): # check if symmetric 142 | absdiff = abs(init_coeffs[i] - right_half[filter_delay-i]) 143 | if absdiff > 1e-2: 144 | warnings.warn("inital filter coefficients are not symmetric: absolute difference = {}".format(absdiff)) 145 | 146 | if opt == True: 147 | h_vars = tf.Variable(right_half, dtype=tf.float32) 148 | else: 149 | h_vars = tf.constant(right_half, tf.float32) 150 | 151 | hmasked = h_vars*mask # apply binary mask for pruning 152 | return tf.concat([tf.reverse(hmasked[1:], axis=[0]), hmasked], axis=0) 153 | 154 | def tf_complex_symmetric_filter(init_coeffs, opt=False, mask=1): 155 | """ Create odd-length symmetric FIR filter with complex coefficients 156 | 157 | For complex-valued symmetric filters of length 2*L+1, there are 2*(L+1) tunable parameters: 158 | real coeffs: h_L, ..., h_1, [h_0, h_1, ..., h_L] 159 | imag coeffs: g_L, ..., g_1, [g_0, g_1, ..., g_L] 160 | 161 | Args: 162 | init_coeffs: complex numpy array of shape [filter_length,] with initial filter coefficients, 163 | filter_length should be odd 164 | coefficients should be symmetric 165 | opt: (Optional) True for variable, False for constant, default=False 166 | mask: (Optional) binary mask for pruning, default=1 167 | 168 | Returns: 169 | A Tensor with shape = [filter_length, 2] 170 | column 1: real coefficients 171 | column 2: imaginary coefficients 172 | """ 173 | 174 | h_real = tf_real_symmetric_filter(np.real(init_coeffs), opt, mask) 175 | h_imag = tf_real_symmetric_filter(np.imag(init_coeffs), opt, mask) 176 | return tf.stack([h_real, h_imag], axis=1) 177 | 178 | def cconv(x, h): 179 | """ y = cconv(x, h) uses tf.nn.conv1d to perform circular convolution of signal x with filter h 180 | 181 | Notes: 182 | - This function also circularly shifts y to remove the filter delay caused by h 183 | - The delay is (filter_length-1)/2 for odd-length filters and filter_length/2-1 for even-length filters 184 | - TensorFlow does not do convolution but correlation, so the filter "flipping" 185 | is performed manually in this function. 186 | 187 | Args: 188 | x: signal Tensor 189 | shape(x) = [batch_size, N] real signal 190 | shape(x) = [batch_size, N, 2] complex signal 191 | h: filter Tensor 192 | shape(h) = [filter_length] real filter 193 | shape(h) = [filter_length, 2] complex filter 194 | 195 | Returns: 196 | A Tensor with shape 197 | shape(y) = [batch_size, N] if both x and h are real 198 | shape(y) = [batch_size, N, 2] all other cases 199 | """ 200 | 201 | filter_length = int(h.shape[0]) 202 | batch_size = int(x.shape[0]) 203 | N = int(x.shape[1]) 204 | 205 | # expand dimensions in case of real signal and/or filter 206 | if len(x.shape) == 2: 207 | x = tf.expand_dims(x, axis=2) 208 | 209 | if len(h.shape) == 1: 210 | h = tf.expand_dims(h, axis=1) 211 | 212 | # extend x to achieve circular convolution and remove the filter delay 213 | if(filter_length%2==0): # even-length filters 214 | filter_delay = filter_length//2-1 215 | x = tf.concat([x[:, N-filter_delay-1:, :], x, x[:, :filter_delay, :]], axis=1) # [x_end, x, x_begin] 216 | else: # odd-length filters 217 | filter_delay = (filter_length-1)//2 218 | x = tf.concat([x[:, N-filter_delay:, :], x, x[:, :filter_delay, :]], axis=1) # [x_end, x, x_begin] 219 | 220 | # reshape filter 221 | if(x.shape[2] == 1 and h.shape[1] == 1): # real signal, real filter 222 | h = tf.reshape(h, [filter_length, 1, 1]) 223 | conv1d_filter = tf.reverse(h, axis=[0]) # flip 224 | elif(x.shape[2] == 1 and h.shape[1] == 2): # real signal, complex filter 225 | hr = tf.reshape(h[:,0], [filter_length, 1, 1]) 226 | hr = tf.reverse(hr, axis=[0]) # flip 227 | hi = tf.reshape(h[:,1], [filter_length, 1, 1]) 228 | hi = tf.reverse(hi, axis=[0]) # flip 229 | conv1d_filter = tf.concat([hr, hi], axis=2) 230 | elif(x.shape[2] == 2 and h.shape[1] == 1): # complex signal, real filter 231 | hr = tf.reshape(h[:,0], [filter_length, 1, 1]) 232 | hr = tf.reverse(hr, axis=[0]) # flip 233 | z = tf.zeros(shape=[filter_length, 1, 1]) # dummy 234 | filter_1 = tf.concat([hr, z], axis=1) 235 | filter_2 = tf.concat([z , hr], axis=1) 236 | conv1d_filter = tf.concat([filter_1, filter_2], axis=2) 237 | elif(x.shape[2] == 2 and h.shape[1] == 2): # complex signal, complex filter 238 | hr = tf.reshape(h[:,0], [filter_length, 1, 1]) 239 | hr = tf.reverse(hr, axis=[0]) # flip 240 | hi = tf.reshape(h[:,1], [filter_length, 1, 1]) 241 | hi = tf.reverse(hi, axis=[0]) # flip 242 | filter_1 = tf.concat([hr, -hi], axis=1) 243 | filter_2 = tf.concat([hi, hr], axis=1) 244 | conv1d_filter = tf.concat([filter_1, filter_2], axis=2) 245 | else: 246 | raise ValueError("signal or filter has wrong shape") 247 | 248 | # call conv1d 249 | # input has shape = [batch_size, N, in_channels] 250 | # filter has shape = [filter_length, in_channels, out_channels] 251 | # output has shape = [batch_size, N, out_channels] 252 | y = tf.nn.conv1d(x, conv1d_filter, stride=1, padding="VALID", name='conv1d') 253 | 254 | if y.shape[2] == 1: # real signal and filter 255 | return y[:,:,0] 256 | else: 257 | return y 258 | 259 | def periodically_extend(x, M): 260 | """ Extends a numpy vector of length N to length M>N by periodically copying the elements """ 261 | N = x.shape[0] 262 | y = np.zeros(M, dtype=x.dtype); 263 | for i in range(M): 264 | y[i] = x[i%N] 265 | return y 266 | 267 | def line2array(line): 268 | ''' converts a string of comma-separated numbers to numpy array ''' 269 | return np.array([float(v) for v in line.strip().split(",")]) 270 | 271 | def effective_length(length, alpha_lin): 272 | if alpha_lin == 0: 273 | return length 274 | else: 275 | return (1-np.exp(-alpha_lin*length))/alpha_lin 276 | 277 | class ssfm_parameters: 278 | """ handles parameters related to the split-step Fourier method (SSFM) 279 | 280 | Initialization is performed with a dictionary that should have the following keys: 281 | step_size_method 282 | logarithmic 283 | linear 284 | step_size 285 | predefined 286 | StPS: steps per span (only for logarithmic and linear) 287 | adjusting_factor: recommended is 0.4 (only for logarithmic) 288 | ssfm_method 289 | symmetric: linear->nonlinear->linear 290 | asymmetric: linear->nonlinear 291 | combine_half_steps: wether to combine half-steps of adjacent spans (only for symmetric) 292 | alpha: attenuation parameter; should be 0 for less steps than spans 293 | beta2: dispersion parameter 294 | gamma: nonlinear parameter 295 | Nsp: number of spans 296 | Lsp: span length [m] 297 | fsamp: sampling frequency 298 | Nsamp: length of the assumed FFT 299 | direction: +1 for forward, -1 for backpropagation 300 | 301 | computed attributes: 302 | model_steps 303 | cd_length 304 | nl_param 305 | nl_length (not used) 306 | 307 | Usage example: 308 | 309 | bw = ssfm_parameters(parameter_dict) 310 | for NN in range(bw.model_steps): 311 | u = sp.ifft(bw.get_cd_filter_freq(NN)*sp.fft(u)) 312 | u = u*np.exp(1J*bw.nl_param[NN]*np.abs(u)**2) 313 | """ 314 | 315 | def __init__(self, opts): 316 | self.__dict__.update(opts) # converts all dictionary entries to attributes 317 | 318 | alpha_lin = self.alpha/(10*np.log10(np.exp(1))) 319 | Nsp = self.Nsp 320 | Lsp = self.Lsp 321 | direction = self.direction 322 | 323 | if direction == +1 and self.Nsp > 1: 324 | raise ValueError("forward propagation valid only for 1 span") 325 | 326 | if self.step_size_method == 'logarithmic': 327 | if 'adjusting_factor' not in opts: 328 | self.adjusting_factor = 0.4 # 0: linear, 1: very logarithmic 329 | 330 | if 'combine_half_steps' not in opts: 331 | self.combine_half_steps = True 332 | 333 | if self.step_size_method == 'step_size': # used only for subband processing 334 | step_size = self.step_size 335 | Ltot = Lsp*Nsp 336 | model_steps = int(np.floor(Ltot/step_size)+1) 337 | last_step_size = Ltot - (model_steps-1)*step_size 338 | 339 | cd_length = step_size*np.ones(model_steps) 340 | cd_length[model_steps-1] = last_step_size 341 | 342 | tmp = np.mod(np.cumsum(cd_length), Lsp) 343 | len_before = np.zeros(model_steps) 344 | len_after = np.zeros(model_steps) 345 | amplifier_location = np.zeros(model_steps) 346 | for NN in range(1, model_steps): 347 | if(tmp[NN-1] > tmp[NN]): 348 | amplifier_location[NN] = 1; 349 | len_after[NN] = tmp[NN] 350 | len_before[NN] = cd_length[NN] - len_after[NN] 351 | amplifier_location[0] = 1 352 | amplifier_location[-1] = 0 353 | 354 | nl_length = np.zeros(model_steps) 355 | eff_len_before = np.zeros(model_steps) 356 | for NN in range(model_steps): 357 | if (amplifier_location[NN] == 1) and (NN != 0): 358 | h = len_after[NN] 359 | eff_len_before[NN] = effective_length(len_before[NN],np.abs(alpha_lin)) 360 | else: 361 | h = cd_length[NN] 362 | nl_length[NN] = effective_length(h,np.abs(alpha_lin)) 363 | else: 364 | StPS = self.StPS 365 | # ====================================================== # 366 | # compute step sizes for one span 367 | # ====================================================== # 368 | if self.step_size_method == 'logarithmic': 369 | alpha_adj = self.adjusting_factor*alpha_lin 370 | delta = (1-np.exp(-alpha_adj*Lsp))/StPS 371 | if(direction == -1): 372 | nn = np.arange(StPS)+1 # 1,2,...,StPS 373 | else: 374 | nn = StPS-np.arange(StPS) # StPS,...,2,1 375 | step_size = -1/(alpha_adj) * np.log((1-(StPS-nn+1)*delta)/(1-(StPS-nn)*delta)) 376 | elif self.step_size_method == "linear": 377 | step_size = Lsp/StPS*np.ones(StPS) 378 | else: 379 | raise ValueError("wrong step_size_method given (should be 'linear' or 'logarithmic'): "+self.step_size_method) 380 | # ====================================================== # 381 | # compute cd_length, nl_length, amplifier_location 382 | # ====================================================== # 383 | if self.ssfm_method == "symmetric": 384 | if self.combine_half_steps == True: 385 | model_steps = Nsp*StPS+1 386 | cd_length = np.zeros(model_steps) 387 | nl_length = np.zeros(model_steps) 388 | for NN in range(Nsp): 389 | for MM in range(StPS): 390 | cd_length[NN*StPS+MM] = step_size[MM]/2 + step_size[(MM+StPS-1)%StPS]/2 391 | nl_length[NN*StPS+MM] = step_size[MM] 392 | cd_length[0] = step_size[0]/2 393 | cd_length[model_steps-1] = step_size[StPS-1]/2 394 | 395 | amplifier_location = np.zeros(model_steps) 396 | amplifier_location[:-1:StPS] = 1 397 | else: 398 | model_steps = Nsp*(StPS+1) 399 | cd_length = np.concatenate([[step_size[0]/2], (step_size[0:-1]+step_size[1:])/2, [step_size[-1]/2]]) 400 | cd_length = np.tile(cd_length, Nsp) 401 | nl_length = np.concatenate([step_size, [0]]) 402 | nl_length = np.tile(nl_length, Nsp) 403 | 404 | amplifier_location = np.zeros(model_steps) 405 | amplifier_location[::StPS+1] = 1 406 | elif self.ssfm_method == "asymmetric": 407 | model_steps = Nsp*StPS 408 | cd_length = np.zeros(model_steps) 409 | nl_length = np.zeros(model_steps) 410 | for NN in range(Nsp): 411 | for MM in range(StPS): 412 | cd_length[NN*StPS+MM] = step_size[MM] 413 | nl_length[NN*StPS+MM] = effective_length(step_size[MM], np.abs(alpha_lin)) 414 | 415 | amplifier_location = np.zeros(model_steps) 416 | amplifier_location[::StPS] = 1 417 | else: 418 | raise ValueError("wrong split step method given (should be 'symmetric' or 'asymmetric'): "+self.ssfm_method) 419 | # ====================================================== # 420 | # compute attenuation and nl_param 421 | # ====================================================== # 422 | nl_param = direction*self.gamma*nl_length 423 | 424 | attenuation = np.exp(-direction*alpha_lin*cd_length/2) 425 | for NN in range(model_steps): 426 | if direction == -1 and amplifier_location[NN] == 1: 427 | attenuation[NN] = attenuation[NN] * np.exp(direction*alpha_lin*Lsp/2) 428 | 429 | # re-normalize nl_param 430 | for NN in range(model_steps): 431 | nl_param[NN] = nl_param[NN]*np.prod(attenuation[0:NN+1:])**2 432 | 433 | if self.step_size_method == "step_size": 434 | for NN in range(model_steps): 435 | if amplifier_location[NN] == 1: 436 | nl_param[NN] = nl_param[NN] + direction*self.gamma*eff_len_before[NN] 437 | 438 | self.model_steps = model_steps 439 | self.cd_length = cd_length 440 | self.nl_length = nl_length 441 | self.nl_param = nl_param 442 | 443 | N = self.Nsamp 444 | self.fvec = np.concatenate((np.linspace(0,N//2-1,N//2), np.linspace(-N//2,-1,N//2))) * self.fsamp/N 445 | 446 | def get_cd_filter_freq(self, NN): 447 | return np.exp(1j*(self.beta2/2)*(2*np.pi*self.fvec)**2*(self.direction*self.cd_length[NN])) 448 | 449 | def ordered_direct_product(A,B): 450 | p = A.shape[0] 451 | q = B.shape[0] 452 | n = A.shape[1] 453 | m = B.shape[1] 454 | 455 | C = np.zeros([p*q,n+m]) 456 | for i in range(q): 457 | C[i*q:(i+1)*q:, :n:] = A[i,:] 458 | for i in range(p): 459 | C[i*q:(i+1)*q:, n::] = B 460 | return C 461 | 462 | def QAM(M): 463 | Msqrt = (np.sqrt(M)).astype(np.int) 464 | if Msqrt**2 != M: 465 | raise ValueError("M has to be of the form M=4^m where m>0") 466 | x_pam = np.expand_dims(-(Msqrt-2*np.arange(start=1, stop=Msqrt+1)+1), axis=1) 467 | x_qam = ordered_direct_product(x_pam, x_pam) 468 | const = x_qam[:,0] + 1j * x_qam[:,1] 469 | return const/np.sqrt(np.mean(np.abs(const)**2)) 470 | 471 | # }}} 472 | #========================================================# 473 | # parse function arguments {{{ 474 | #========================================================# 475 | parser = ap.ArgumentParser("python3 ldbp.py") 476 | parser.description = "Learned Digital Backpropagation (LDBP)" 477 | parser.add_argument("P", help="set of training powers in dB, e.g., [5] or [5,6,7]") 478 | parser.add_argument("Lr", help="learning rate, e.g., 0.01") 479 | parser.add_argument("iter", help="gradient descent iterations, e.g., 1000") 480 | parser.add_argument("-c", "--config_path", help="path to configuration file (default is ldbp_config.ini)", default="ldbp_config.ini") 481 | parser.add_argument("-l", "--logdir", help="directory for log files (default is log)", default="log") 482 | parser.add_argument("-t", "--timing", help="time the forward propagation", action="store_true") 483 | 484 | args = parser.parse_args() 485 | args_dict = vars(args) # converts to a dictionary 486 | 487 | opt_list="P,Lr,iter".split(",") 488 | arg_str = "" 489 | for i in range(len(opt_list)): 490 | arg_str += opt_list[i] 491 | arg_str += args_dict[opt_list[i]] 492 | if(i != len(opt_list)-1): 493 | arg_str += "_" 494 | 495 | config_path = args.config_path 496 | P_dB_r = np.asarray(eval(args.P)) 497 | P_W_r = pow(10, P_dB_r/10)*1e-3 498 | iterations = int(args.iter) 499 | learning_rate = float(args.Lr) 500 | 501 | # }}} 502 | #========================================================# 503 | # read config file {{{ 504 | #========================================================# 505 | defaults = { 506 | # system 507 | "sigma scaling" : "1", 508 | "modulation" : "16-QAM", 509 | # LDBP 510 | "combine half-steps" : "yes", 511 | "load cd filter" : "no", 512 | "load cd filter filename" : "parameters.csv", 513 | "optimize cd filters" : "yes", 514 | "optimize Kerr parameters" : "no", 515 | "complex Kerr parameters" : "no", 516 | "tied Kerr parameters" : "no", 517 | "pruning" : "no", 518 | "less steps than spans" : "no", 519 | "cd alpha" : "1", 520 | "cd filter length margin" : "2.0", 521 | "cd filter length minimum" : "13", 522 | "nl alpha" : "1", 523 | "nl filter length" : "1", 524 | # training 525 | "adam_A" : "0.9", # decay for running average of the gradient 526 | "adam_B" : "0.999", # decay for running average of the square of the gradient 527 | "rmsprop_A" : "0.9", 528 | "rmsprop_B" : "0.1", 529 | "adadelta_A" : "0.1", 530 | "adagrad_A" : "0.1", 531 | # data 532 | 'forward step size method' : 'logarithmic', 533 | 'forward split step method' : 'symmetric' 534 | } 535 | 536 | config = configparser.ConfigParser(defaults) 537 | 538 | config_folder, config_file = os.path.split(config_path) 539 | print("configuration file name: '"+config_file+"'") 540 | 541 | if not os.path.exists(config_path): 542 | raise RuntimeError("config file in '"+config_file+"' does not exist") 543 | config.read(config_path) 544 | 545 | # system parameters 546 | conf_sys = config['system parameters'] 547 | Lsp = conf_sys.getfloat('span length [km]')*1.0e3 548 | alpha = conf_sys.getfloat('alpha [dB/km]')*1.0e-3 549 | gamma = conf_sys.getfloat('gamma [1/W/km]')*1.0e-3 550 | noise_figure = conf_sys.getfloat('amplifier noise figure [dB]') 551 | sigma_scaling = conf_sys.getfloat('sigma scaling') 552 | Nsp = conf_sys.getint('number of spans') 553 | fsym = conf_sys.getfloat('symbol rate [Gbaud]')*1.0e9 554 | modulation = conf_sys['modulation'] 555 | rolloff = conf_sys.getfloat('RRC roll-off') 556 | delay = conf_sys.getint('RRC delay') 557 | lp_bandwidth = conf_sys.getfloat('low-pass filter bandwidth [GHz]')*1.0e9 558 | Nsym = conf_sys.getint('data symbols per block') 559 | OS_a = conf_sys.getint('analog oversampling') 560 | OS_d = conf_sys.getint('digital oversampling') 561 | if config.has_option('system parameters', 'D [ps/nm/km]'): 562 | D = conf_sys.getfloat('D [ps/nm/km]')*1.0e-6 563 | beta2 = -D*co_lambda**2/(2*np.pi*co_c0) 564 | else: 565 | beta2 = conf_sys.getfloat('beta2 [ps^2/km]')*1.0e-27 566 | 567 | # LDBP parameters 568 | conf_ldbp = config['LDBP parameters'] 569 | step_size_method_bw = conf_ldbp['step size method'] 570 | ssfm_method_bw = conf_ldbp['split step method'] 571 | combine_half_steps = conf_ldbp['combine half-steps'] 572 | cd_opt = conf_ldbp.getboolean('optimize cd filters') 573 | cd_alpha = conf_ldbp.getfloat('cd alpha') 574 | load_cd_filter = conf_ldbp.getboolean('load cd filter') 575 | if load_cd_filter == True: 576 | cd_filter_filename = conf_ldbp['load cd filter filename'] 577 | else: 578 | cd_filter_method = conf_ldbp['cd filter method'] 579 | cd_filter_bandwidth = conf_ldbp.getfloat('cd filter bandwidth') 580 | cd_filter_oob_gain = conf_ldbp.getfloat('cd filter max out-of-band gain') # 0.58 for 17-taps 20 Gbaud 581 | nl_opt = conf_ldbp.getboolean('optimize Kerr parameters') 582 | if nl_opt == True: 583 | tied_Kerr = conf_ldbp.getboolean('tied Kerr parameters') 584 | nl_alpha = conf_ldbp.getfloat('nl alpha') 585 | nl_filter_length = conf_ldbp.getint('nl filter length') 586 | less_steps_than_spans = conf_ldbp.getboolean('less steps than spans') 587 | 588 | # training parameters 589 | conf_train = config['training'] 590 | minibatch_size = conf_train.getint('minibatch size') 591 | optimizer = conf_train['optimizer'] 592 | summary_interval = conf_train.getint('summary writing interval') 593 | SAVE_FILE = conf_train.getboolean('save results to file') 594 | 595 | # data generation 596 | conf_data = config['data generation'] 597 | StPS_fw = conf_data.getint('forward steps per span') 598 | step_size_method_fw = conf_data['forward step size method'] 599 | ssfm_method_fw = conf_data['forward split step method'] 600 | QMAX = conf_data.getint('number of queue elements') # number of queue elements 601 | QBSIZE = conf_data.getint('generation batch size') # batch size to pupulate the queue 602 | NPROC = conf_data.getint('number of parallel processors') # number of processors used to populate the queue 603 | REPF = conf_data.getint('data replication factor') # replication factor for data 604 | 605 | if OS_a%OS_d != 0: 606 | raise ValueError('oversampling factors have to be divisible: OS_a={}, OS_d={}'.format(OS_a, OS_d)) 607 | 608 | if nl_filter_length%2 == 0: 609 | raise ValueError('nl_filter_length has to be odd: nl_filter_length = {}'.format(nl_filter_length)) 610 | nl_filter_delay = (nl_filter_length-1)//2 611 | 612 | # derived parameters 613 | L = Lsp*Nsp 614 | Gain = 10.0**(alpha*Lsp/10.0) 615 | sef = 10.0**(noise_figure/10.0)/2.0/(1.0-1.0/Gain) 616 | alpha_lin = alpha / dB_conv 617 | N0 = sigma_scaling*Nsp*(np.exp(alpha_lin*Lsp)-1.0)*co_h*nu*sef 618 | sigma2 = N0 * fsym * OS_a 619 | Nsamp_a = Nsym*OS_a 620 | Nsamp_d = Nsym*OS_d 621 | fsamp_a = fsym*OS_a 622 | fsamp_d = fsym*OS_d 623 | f_a = get_fvec(Nsamp_a, fsamp_a) 624 | f_d = get_fvec(Nsamp_d, fsamp_d) 625 | 626 | if "QAM" in modulation: 627 | splitstr = modulation.split("-") 628 | modulation_order = int(splitstr[0]) 629 | modulation = "QAM" 630 | 631 | print("total memory of the data queue: {} MB".format(QMAX*64*(Nsamp_d+Nsym)/8/1e6)) 632 | 633 | # }}} 634 | #========================================================# 635 | # forward propagation generative model {{{ 636 | #========================================================# 637 | ps_filter_tx_coeffs = rrcosine(rolloff, delay, OS_a) # pulse shaping filter 638 | ps_filter_tx_length = 2*(OS_a*delay)+1 639 | ps_filter_tx_delay = OS_a*delay # delay in samples 640 | 641 | # pre-compute frequency responses 642 | ps_tmp = np.concatenate((ps_filter_tx_coeffs, np.zeros(Nsamp_a-ps_filter_tx_length))) 643 | ps_tmp = np.roll(ps_tmp, -ps_filter_tx_delay) 644 | ps_filter_tx_freq = sp.fft(ps_tmp, n=Nsamp_a) 645 | lp_filter_freq = (abs(f_a) <= lp_bandwidth/2).astype(float) 646 | 647 | if modulation == "QAM": 648 | const = QAM(modulation_order) 649 | 650 | ssfm_opts = { 651 | "alpha": alpha, 652 | "beta2": beta2, 653 | "gamma": gamma, 654 | "Nsp": 1, 655 | "Lsp": Lsp, 656 | "fsamp": fsamp_a, 657 | "Nsamp": Nsamp_a, 658 | "step_size_method": step_size_method_fw, 659 | "ssfm_method": ssfm_method_fw, 660 | "StPS": StPS_fw, 661 | "direction": 1 662 | } 663 | 664 | fw = ssfm_parameters(ssfm_opts) 665 | 666 | def forward_propagation(): 667 | """ 668 | Returns: 669 | y: received signal (shape = [Nsamp_d, 2], separate real and imaginary part) 670 | x: symbol vector (shape = [Nsym], complex) 671 | P: launch power (in W) 672 | """ 673 | np.random.seed() # new seed is necessary for multiprocessor 674 | P = P_W_r[np.random.randint(P_W_r.shape[0])] # get random launch power 675 | # [SOURCE] random points from the signal constellation 676 | if modulation == "QAM": 677 | x = const[np.random.randint(const.shape[0], size=[1, Nsym])] 678 | elif modulation == "Gaussian": 679 | x = (np.random.normal(0,1,size=[1, Nsym]) + 1j*np.random.normal(0,1,size=[1, Nsym]))/np.sqrt(2) 680 | else: 681 | raise ValueError("wrong modulation format: " + modulation) 682 | # [MODULATION] upsample + pulse shaping 683 | x_up = np.zeros([1, Nsamp_a], dtype=np.complex64) 684 | x_up[:, ::OS_a] = x*np.sqrt(OS_a) 685 | u = sp.ifft(sp.fft(x_up)*ps_filter_tx_freq)*np.sqrt(P) 686 | # [CHANNEL] simulate forward propagation 687 | for NN in range(Nsp): # enter a span 688 | for MM in range(fw.model_steps): # enter a segment 689 | u = sp.ifft(fw.get_cd_filter_freq(MM)*sp.fft(u)) 690 | u = u*np.exp(1j*fw.nl_param[MM]*np.abs(u)**2) 691 | #u = u*np.exp(1j*(8/9)*fw.nl_param[MM]*(np.abs(u[0,:])**2+np.abs(u[1,:])**2)) 692 | # add noise, NOTE: amplifier gain (u = u*np.exp(alpha_lin*Lsp/2.0)) is absorbed in nl_param 693 | u = u + np.sqrt(sigma2/2/Nsp) * (np.random.randn(1,Nsamp_a) + 1j*np.random.randn(1,Nsamp_a)) 694 | # [RECEIVER] low-pass filter + downsample 695 | u = sp.ifft(sp.fft(u)*lp_filter_freq) 696 | y = u[0, ::OS_a//OS_d] 697 | y = np.stack([np.real(y), np.imag(y)], axis=1) 698 | return y, x[0,:], P 699 | 700 | if args.timing == True: 701 | print("") 702 | print("timing the forward propation ...") 703 | t = time.time() 704 | _,_,_ = forward_propagation() 705 | elapsed = time.time()-t 706 | print("{0:.2f} seconds to generate 1 input/output data pair".format(elapsed)) 707 | print("Generating approx. {0:.0f} input/output data pairs per seconds".format(NPROC*REPF/elapsed)) 708 | sys.exit("") 709 | 710 | # }}} 711 | #========================================================# 712 | # compute step sizes for DBP {{{ 713 | #========================================================# 714 | ssfm_opts = {} 715 | ssfm_opts['beta2'] = beta2 716 | ssfm_opts['gamma'] = gamma 717 | ssfm_opts['fsamp'] = fsamp_d 718 | ssfm_opts['Nsamp'] = Nsamp_d 719 | ssfm_opts['step_size_method'] = step_size_method_bw 720 | ssfm_opts['ssfm_method'] = ssfm_method_bw 721 | ssfm_opts['combine_half_steps'] = combine_half_steps 722 | ssfm_opts['direction'] = -1 723 | 724 | if less_steps_than_spans == False: 725 | ssfm_opts['alpha'] = alpha 726 | ssfm_opts['Nsp'] = Nsp 727 | ssfm_opts['Lsp'] = Lsp 728 | ssfm_opts['StPS'] = int(conf_ldbp['steps per span']) 729 | else: 730 | ssfm_opts['alpha'] = 0 731 | ssfm_opts['Nsp'] = 1 732 | ssfm_opts['Lsp'] = Lsp*Nsp 733 | ssfm_opts['StPS'] = int(conf_ldbp['total steps']) 734 | 735 | bw = ssfm_parameters(ssfm_opts) 736 | 737 | #}}} 738 | #========================================================# 739 | # compute or load initial cd filter coefficients {{{ 740 | #========================================================# 741 | cd_filter_coeffs = {} 742 | 743 | if load_cd_filter == False: 744 | # create object for cd filter design 745 | fopt = {} 746 | fopt['beta2'] = beta2 747 | fopt['fsamp'] = fsamp_d 748 | fopt['Nsamp'] = Nsamp_d 749 | fopt['method'] = cd_filter_method 750 | fopt['bandwidth'] = cd_filter_bandwidth 751 | fopt['max_out_of_band_gain'] = cd_filter_oob_gain # 0.58 for 17-taps 20 Gbaud 752 | fir_obj = fir.cd_fir_filter(fopt) 753 | 754 | # determine length of cd filters 755 | if config.has_option('LDBP parameters', 'cd filter length'): 756 | tmp = (line2array(conf_ldbp['cd filter length'])).astype(np.int32) 757 | cd_filter_length = periodically_extend(tmp, bw.model_steps) # tile 758 | else: 759 | cd_filter_length_margin = float(conf_ldbp['cd filter length margin']) 760 | cd_filter_length_min = int(conf_ldbp['cd filter length minimum']) 761 | print("No cd filter length provided. Computing automatically with margin {} and minimum {}:".format(cd_filter_length_margin,cd_filter_length_min)) 762 | 763 | req_len = fir_obj.get_required_filter_length(bw.cd_length) 764 | cd_filter_length = (2*np.ceil(req_len/2*(1+cd_filter_length_margin))+1).astype(np.int32) 765 | for NN in range(bw.model_steps): 766 | if cd_filter_length[NN] < cd_filter_length_min: 767 | cd_filter_length[NN] = cd_filter_length_min 768 | print(cd_filter_length) 769 | 770 | # compute filter taps 771 | for NN in range(bw.model_steps): 772 | cd_filter_coeffs[NN] = fir_obj.get_filter(bw.cd_length[NN], cd_filter_length[NN]) 773 | else: # or load from file 774 | f = open(cd_filter_filename) 775 | lines = f.readlines() 776 | f.close() 777 | 778 | if len(lines) != 3*bw.model_steps: 779 | raise RuntimeError("File '"+cd_filter_filename+"' should have {} lines but has {}".format(3*bw.model_steps, len(lines))) 780 | 781 | cd_filter_length = np.zeros(bw.model_steps, dtype=np.int64) 782 | for NN in range(bw.model_steps): 783 | h_r = line2array(lines[NN*3+0]) 784 | h_i = line2array(lines[NN*3+1]) 785 | if np.size(h_r) != np.size(h_i): 786 | raise ValueError('real and imaginary part of loaded cd filters should be the same') 787 | cd_filter_length[NN] = np.size(h_r) 788 | if cd_filter_length[NN]%2 == 0: 789 | raise ValueError('loaded cd filter should have odd length: cd_filter_length[{}]={}'.format(NN,cd_filter_length[NN])) 790 | cd_filter_coeffs[NN] = h_r+1j*h_i 791 | print("loaded cd filters have lengths:") 792 | print(cd_filter_length) 793 | 794 | cd_filter_delay = (cd_filter_length-1)//2 795 | 796 | #}}} 797 | #========================================================# 798 | # define pruning parameters {{{ 799 | #========================================================# 800 | pruning = config.getboolean('LDBP parameters', 'pruning') 801 | 802 | def get_prune_op(mask, mask_len): 803 | mask_len_descreased = tf.assign(mask_len, mask_len-1) 804 | pos = tf.constant(np.arange(0, int(mask.get_shape()[0]), 1, np.int32), tf.int32) 805 | new_mask = tf.cast(tf.less(pos, mask_len_descreased), tf.float32) 806 | return tf.assign(mask, new_mask) 807 | 808 | cd_mask = {} 809 | 810 | if pruning == True: 811 | # get target lengths and memory 812 | tmp = (line2array(conf_ldbp['target cd filter length'])).astype(np.int32) 813 | target_length = periodically_extend(tmp, bw.model_steps) # tile 814 | for NN in range(bw.model_steps): 815 | if target_length[NN] < 0: 816 | target_length[NN] = cd_filter_length[NN] + target_length[NN] 817 | if target_length[NN]%2 == 0: 818 | raise ValueError("target filter lengths have to be odd") 819 | print("pruned filters will have lengths:") 820 | print(target_length) 821 | target_delay = (target_length-1)//2 822 | # determine the pruning order 823 | prune_order = [] 824 | max_len = np.max(cd_filter_delay) 825 | min_len = np.min(target_delay) 826 | for i in range(max_len-min_len+1): 827 | for NN in range(bw.model_steps): 828 | if cd_filter_delay[NN] >= max_len-i and target_delay[NN] < max_len-i: 829 | prune_order.append(NN) 830 | # shuffle the order 831 | random.shuffle(prune_order) 832 | # determine pruning schedule: train-prune-train-prune-train 833 | pruning_steps = len(prune_order) 834 | #pruning_interval = np.ceil(iterations/(pruning_steps+1)) 835 | print("total pruning steps: {}".format(pruning_steps)) 836 | pruning_schedule = (np.ceil(np.ceil(2.0**(-np.arange(pruning_steps,0,-1))*iterations))) 837 | pruning_schedule = np.ceil(pruning_schedule + np.arange(pruning_steps)*iterations/8/pruning_steps) 838 | 839 | cd_mask_len = {} 840 | prune_op = {} # each mask has a pruning op associated with it 841 | 842 | for NN in range(bw.model_steps): 843 | cd_mask[NN] = tf.Variable(np.ones([cd_filter_delay[NN]+1]), dtype=tf.float32, trainable=False) 844 | cd_mask_len[NN] = tf.Variable(cd_filter_delay[NN]+1, dtype=tf.int32, trainable=False) 845 | prune_op[NN] = get_prune_op(cd_mask[NN], cd_mask_len[NN]) 846 | else: 847 | print("no pruning") 848 | for NN in range(bw.model_steps): 849 | cd_mask[NN] = 1 850 | 851 | # }}} 852 | #========================================================# 853 | # define tunable parameters {{{ 854 | #========================================================# 855 | no_filter = np.zeros(nl_filter_length, dtype=np.float32) 856 | no_filter[nl_filter_delay] = 1.0 857 | 858 | if nl_opt == True and tied_Kerr == True: 859 | nl_filter_all = tf_real_symmetric_filter(no_filter*nl_alpha, nl_opt) 860 | 861 | cd_filter = {} 862 | nl_filter = {} 863 | 864 | for NN in range(bw.model_steps): 865 | # linear parameters 866 | cd_filter[NN] = tf_complex_symmetric_filter(cd_filter_coeffs[NN]*cd_alpha, cd_opt, mask=cd_mask[NN]) 867 | # nonlinear parameters 868 | if nl_opt == True: 869 | if tied_Kerr == True: 870 | nl_filter[NN] = nl_filter_all 871 | else: 872 | nl_filter[NN] = tf_real_symmetric_filter(no_filter*nl_alpha, nl_opt) 873 | else: 874 | nl_filter[NN] = tf_real_symmetric_filter(no_filter*nl_alpha, nl_opt) 875 | 876 | # matched filter 877 | ps_filter = tf_real_symmetric_filter(rrcosine(rolloff, delay, OS_d)) 878 | 879 | # }}} 880 | #========================================================# 881 | # build the computation graph in TensorFlow {{{ 882 | #========================================================# 883 | print("building the TensorFlow graph ", end='', flush=True) 884 | 885 | y_enq = tf.placeholder(tf.float32, shape=[None, Nsamp_d, 2]) 886 | x_enq = tf.placeholder(tf.complex64, shape=[None, Nsym]) 887 | P_enq = tf.placeholder(tf.float32, shape=[None, 1]) 888 | 889 | min_after_dequeue = int(conf_data["minimum elements after dequeue"]) # at least this many elements must remain after dequeue 890 | 891 | myq = tf.RandomShuffleQueue(QMAX, min_after_dequeue, dtypes=[tf.float32, tf.complex64, tf.float32], shapes=[[Nsamp_d, 2], [Nsym], [1]]) 892 | enqueue_op = myq.enqueue_many([y_enq, x_enq, P_enq]) 893 | dummy_dequeue = myq.dequeue_many(QBSIZE*NPROC*REPF) 894 | y,x,P_W = myq.dequeue_many(minibatch_size) 895 | 896 | # [LDBP], signals have shape = [batch_size, N, 2] (if complex) or [batch_size, N] (if real) 897 | for NN in range(bw.model_steps): 898 | print('.', end='', flush=True) 899 | # linear step 900 | y = cconv(y, cd_filter[NN]/cd_alpha) # complex(y) = complex(x) * complex(h) 901 | # nonlinear step, includes possible filtering of {|y_i|^2} 902 | #ysq = bw.nl_param[NN]*tf.reduce_sum(tf.square(y), axis=2) 903 | ysq = bw.nl_param[NN]*tf.reduce_sum(tf.square(y), axis=2) 904 | ysq_filtered = cconv(ysq, nl_filter[NN]/nl_alpha) # real(y) = real(x) * real(h) 905 | y = complex_multiply(y, tf.stack([tf.cos(ysq_filtered), tf.sin(ysq_filtered)], axis=2)) 906 | 907 | # matched filter 908 | y = cconv(y, ps_filter) # complex(y) = complex(x) * real(h) 909 | y = tf.complex(y[:,:,0],y[:,:,1]) 910 | # downsample 911 | y = y[:,::OS_d] / tf.complex(tf.sqrt(P_W), 0.0) / np.sqrt(OS_d) 912 | # constant phase-offset rotation 913 | tmp = tf.reduce_sum(tf.conj(x)*y, 1, keepdims=True) 914 | phi_cpe = -tf.atan2(tf.imag(tmp),tf.real(tmp)) 915 | x_hat = y * tf.exp(tf.complex(0.0, phi_cpe)) 916 | 917 | mean_squared_error = tf.reduce_mean(tf.square(tf.abs(x-x_hat))) 918 | effective_snr = -10.0*tf.log(mean_squared_error+1e-12)/tf.log(10.0) 919 | 920 | print("") 921 | print("calling optimizer ... ", end="", flush=True) 922 | optimizer = get_optimizer() 923 | train = optimizer.minimize(mean_squared_error) 924 | 925 | # compute total number of tunable parameters 926 | total_parameters = 0 927 | for variable in tf.trainable_variables(): 928 | shape = variable.get_shape() # shape is an array of tf.Dimension 929 | variable_parameters = 1 930 | for dim in shape: 931 | variable_parameters *= dim.value 932 | total_parameters += variable_parameters 933 | 934 | print("done, total tunable parameters: {}".format(total_parameters)) 935 | 936 | # }}} 937 | #========================================================# 938 | # start session {{{ 939 | #========================================================# 940 | tf.summary.scalar("effective_snr", effective_snr) 941 | tf.summary.scalar("data_queue_size", myq.size()) 942 | summary = tf.summary.merge_all() 943 | 944 | init_op = tf.global_variables_initializer() 945 | sess = tf.Session() 946 | 947 | # create log dir 948 | logdir = args.logdir+"/"+arg_str 949 | logdir += "/" + time.strftime("%Y-%m-%d_%H.%M.%S", time.gmtime()) 950 | 951 | if not os.path.exists(logdir): 952 | os.makedirs(logdir) 953 | else: 954 | raise RuntimeError("log directory \'" + logdir + "\' already exists") 955 | 956 | print("name of the log directory: " + logdir) 957 | 958 | # copy the .ini file to log folder 959 | shutil.copyfile(config_path, logdir+"/"+config_file) 960 | summary_writer = tf.summary.FileWriter(logdir, sess.graph) 961 | sess.run(init_op) # run the OP that initializes global variables 962 | 963 | # }}} 964 | #========================================================# 965 | # populate the data queue {{{ 966 | #========================================================# 967 | def forward_propagation_batch(ignore_arg): 968 | y_read = np.zeros([QBSIZE, Nsamp_d, 2], np.float32) 969 | x_read = np.zeros([QBSIZE, Nsym], np.complex64) 970 | P_read = np.zeros([QBSIZE, 1], np.float32) 971 | for i in range(QBSIZE): 972 | y_read[i,:,:], x_read[i,:], P_read[i,:] = forward_propagation() 973 | return y_read, x_read, P_read 974 | 975 | def populate_queue(sess, enqueue_op, coord): 976 | m = multiprocessing.cpu_count() 977 | pool = multiprocessing.Pool(m) 978 | while not coord.should_stop(): 979 | results = pool.map(forward_propagation_batch, [0]*NPROC) 980 | y_batch = np.zeros([QBSIZE*NPROC*REPF, Nsamp_d, 2], np.float32) 981 | x_batch = np.zeros([QBSIZE*NPROC*REPF, Nsym], np.complex64) 982 | P_batch = np.zeros([QBSIZE*NPROC*REPF, 1], np.float32) 983 | for j in range(REPF): 984 | for i in range(NPROC): 985 | off = j*QBSIZE*NPROC 986 | y_batch[i*QBSIZE+off:(i+1)*QBSIZE+off,:,:] = results[i][0] 987 | x_batch[i*QBSIZE+off:(i+1)*QBSIZE+off,:] = results[i][1] 988 | P_batch[i*QBSIZE+off:(i+1)*QBSIZE+off,:] = results[i][2] 989 | sess.run(enqueue_op, feed_dict={y_enq: y_batch, x_enq: x_batch, P_enq: P_batch}) 990 | 991 | coord = tf.train.Coordinator() 992 | t = threading.Thread(target=populate_queue, args=(sess, enqueue_op, coord)) 993 | t.start() 994 | 995 | # }}} 996 | #========================================================# 997 | # optimization routine {{{ 998 | #========================================================# 999 | # inital values 1000 | mse_tmp, snr_tmp = sess.run([mean_squared_error, effective_snr]) 1001 | print("---------------------------------------------") 1002 | print("initial: MSE = {0:.6f}, effective SNR = {1:.3f} dB".format(mse_tmp, snr_tmp)) 1003 | print("elements in the data queue: {}".format(sess.run(myq.size()))) 1004 | 1005 | # write initial summary 1006 | sstr = sess.run(summary) 1007 | summary_writer.add_summary(sstr, 0) 1008 | summary_writer.flush() 1009 | 1010 | # gradient descent 1011 | start = time.time() 1012 | 1013 | pruned = 0 1014 | for i in range(1,iterations+1): 1015 | _, mse_tmp, snr_tmp, sstr = sess.run([train, mean_squared_error, effective_snr, summary]) # 1 step in gradient descent 1016 | if(math.isnan(mse_tmp)): 1017 | print("nan detected, exiting optimization loop") 1018 | break 1019 | # summary 1020 | if i%summary_interval == 0 or i==iterations: 1021 | summary_writer.add_summary(sstr, i) 1022 | summary_writer.flush() 1023 | print("iter {0}: MSE = {1:.6f}, effective SNR = {2:.3f} dB, summary written".format(i, mse_tmp, snr_tmp)) 1024 | # pruning 1025 | if pruning == True: 1026 | pr_cnt = (pruning_schedule == i).sum() 1027 | for II in range(pr_cnt): 1028 | print("pruning (iter: {}, filter: {}, progress: {}/{})".format(i,prune_order[pruned],pruned+1,pruning_steps)) 1029 | sess.run(prune_op[prune_order[pruned]]) 1030 | pruned = pruned + 1 1031 | 1032 | end = time.time() 1033 | 1034 | print("requesting stop") 1035 | coord.request_stop() 1036 | 1037 | queue_size = sess.run(myq.size()) 1038 | if(QMAX - queue_size < QBSIZE*NPROC*REPF): 1039 | print("dummy dequeue") 1040 | sess.run(dummy_dequeue) # otherwise the threads hang at enqueue_op 1041 | 1042 | print("joining threads") 1043 | coord.join([t]) # wait for threads to terminate 1044 | 1045 | opt_time = end-start 1046 | print("total optimization time: {0:.1f}s".format(opt_time)) 1047 | print("processing approx. {0:.0f} input/output data pairs per second".format(iterations*minibatch_size/opt_time)) 1048 | 1049 | # }}} 1050 | #========================================================# 1051 | # save results to csv file {{{ 1052 | #========================================================# 1053 | if SAVE_FILE == True: 1054 | print("saving optimized parameters ... ", end="", flush=True) 1055 | 1056 | f=open(logdir+'/parameters.csv', 'ab') # a: append, b: binary mode 1057 | f.truncate(0) 1058 | 1059 | cd_filter_print = sess.run(cd_filter) 1060 | nl_filter_print = sess.run(nl_filter) 1061 | 1062 | for NN in range(bw.model_steps): 1063 | tmp = np.transpose(cd_filter_print[NN] / cd_alpha) 1064 | if pruning == True: # only store the pruned filter 1065 | delay_diff = cd_filter_delay[NN] - target_delay[NN] 1066 | tmp = tmp[:,delay_diff:delay_diff+target_length[NN]:] 1067 | np.savetxt(f, tmp, delimiter=',') 1068 | np.savetxt(f, np.transpose(nl_filter_print[NN]*bw.nl_param[NN] / nl_alpha), delimiter=',') 1069 | f.close() 1070 | print("done") 1071 | else: 1072 | print("nothing is saved ...") 1073 | 1074 | # }}} 1075 | #========================================================# 1076 | -------------------------------------------------------------------------------- /ldbp/lib/fir.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from numpy import pi, cos, sin, exp, sqrt 3 | from scipy import special, linalg, optimize, ifft 4 | import warnings 5 | #import sympy as sym 6 | 7 | class cd_fir_filter: 8 | """ finite impulse response (FIR) filters for compensating chromatic dispersion (CD) 9 | 10 | Second-order dispersion is modeled in the (forward) split-step Fourier method as 11 | 12 | u = -j beta2/2 d^2u/dt^2 13 | 14 | which has the frequency-domain solution 15 | 16 | U(w,z) = exp(j beta/2 z w^2) U(w,0) 17 | 18 | In order to compensate, the ideal filter response is given by 19 | 20 | EDC(w) = exp(-j beta/2 z w^2) 21 | 22 | Args (provided as a dictionary): 23 | beta2: dispersion parameter 24 | fsamp: sampling frequency in Hz 25 | Nsamp: number of samples (for FFT) 26 | method: employed filter-design method 27 | 'truncate IDFT' 28 | 'direct sampling' 29 | 'least squares' 30 | 'LS-CO': least squares with constrained out-of-band gain 31 | bandwidth: percentage of compensated bandwidth, between 0 and 1, defaults to 1 32 | only used for 'least squares' and 'LS-CO' 33 | eps: regularization parameter for 'least squares', defaults to 1e-12 34 | max_out_of_band_gain: for 'LS-CO' 35 | """ 36 | 37 | def __init__(self, opts): 38 | self.__dict__.update(opts) # converts all dictionary entries to attributes 39 | 40 | if 'bandwidth' not in opts: 41 | self.bandwidth = 1.0 42 | if opts['method'] == 'least squares' and 'eps' not in opts: 43 | self.eps = 1e-12 44 | if self.bandwidth < 0.0 or self.bandwidth > 1.0: 45 | raise ValueError("bandwidth parameter has to be between 0 and 1: bandwidth = {}".format(self.bandwidth)) 46 | 47 | def get_required_filter_length(self, L): 48 | L = np.array(L) 49 | Df = self.bandwidth*self.fsamp 50 | return (2*np.floor(2*pi*np.abs(self.beta2)*L*Df*self.fsamp/2)+1).astype(np.int32) 51 | 52 | def get_filter(self, L, cd_filter_length): 53 | """ approximates exp(j*KK*w^2), where w=-pi..pi 54 | 55 | Args: 56 | L: fiber length [m] 57 | cd_filter_length: length of the FIR filter, has to be odd 58 | 59 | Returns: 60 | Filter coefficients as numpy array 61 | """ 62 | 63 | if cd_filter_length%2 == 0: 64 | raise ValueError("cd_filter_length has to be odd: cd_filter_length={}".format(cd_filter_length)) 65 | 66 | KK = -(self.beta2)/2*L*(self.fsamp**2) 67 | delay = (cd_filter_length-1)//2 68 | N = self.Nsamp 69 | xi = self.bandwidth 70 | Omega1 = -pi*xi 71 | Omega2 = pi*xi 72 | K = int(np.floor(N/2*self.bandwidth)) 73 | 74 | if xi < 1: 75 | i = np.reshape(np.arange(K+1,(N-K-1)+1), [N-2*K-1, 1]) 76 | k = np.reshape(np.arange(-delay, delay+1), [1, cd_filter_length]) 77 | B = exp(-1j*i*k*2*pi/N) * sqrt((2*pi/N)/(2*pi+Omega1-Omega2)) 78 | else: 79 | B = np.zeros([1, cd_filter_length]) 80 | 81 | out_of_band_gain = lambda h: np.sum(np.abs(np.matmul(B,h))**2) 82 | 83 | i = np.reshape(np.arange(-K,K+1), [2*K+1, 1]) 84 | k = np.reshape(np.arange(-delay, delay+1), [1, cd_filter_length]) 85 | A = exp(-1j*i*k*2*pi/N) #* sqrt((2*pi/N)/(Omega2-Omega1)) 86 | des = exp(1j*KK*(2*pi*i/N)**2) #* sqrt((2*pi/N)/(Omega2-Omega1)) 87 | 88 | inband_error = lambda h: np.sum(np.abs(np.matmul(A,h)-des)**2)#/(2*xi*pi)/N 89 | 90 | if self.method == 'truncate IDFT': 91 | fvec = np.fft.fftshift(np.arange(-N//2, N//2)*self.fsamp/N) 92 | htmp = ifft(exp(-1j*self.beta2/2*L*(2*pi*fvec)**2)) 93 | cd_filter_coeffs = np.concatenate([np.flipud(htmp[1:delay+1:]), htmp[0:delay+1:]]) 94 | elif self.method == 'direct sampling': # Savory (2008) 95 | cd_filter_coeffs = np.zeros([cd_filter_length], dtype=np.complex64) 96 | for n in range(-delay, delay+1): 97 | cd_filter_coeffs[n+delay] = sqrt(1j/(4*KK*pi))*exp(-1j*n**2/(4*KK)) 98 | elif self.method == 'least squares2': 99 | Q = xi*np.ones([delay+1, delay+1]) # Q[0,0] = xi 100 | for i in range(1,delay+1): 101 | Q[0,i] = Q[i,0] = 2*sin(i*pi*xi)/(i*pi) 102 | for i in range(1,delay+1): 103 | for j in range(1,delay+1): 104 | if i != j: 105 | AA = i*cos(j*pi*xi)*sin(i*pi*xi) 106 | BB = j*cos(i*pi*xi)*sin(j*pi*xi) 107 | Q[i,j] = 4*(AA-BB)/(i**2-j**2)/pi 108 | else: 109 | Q[i,i] = (2*pi*xi+sin(2*i*pi*xi)/i)/pi 110 | 111 | nn = np.arange(delay+1) 112 | D = exp(-1j*(nn**2/(4*KK) + 3*pi/4))/(2*sqrt(pi*KK+0j)) \ 113 | *(special.erf(exp(1j*3*pi/4)*(2*Omega2*KK-nn)/(2*sqrt(KK+0j))) \ 114 | + special.erf(exp(1j*3*pi/4)*(2*Omega2*KK+nn)/(2*sqrt(KK+0j)))) 115 | D[0] = D[0]/2 116 | 117 | I = 2*np.eye(delay+1) 118 | I[0,0] = 1 119 | 120 | tmp = linalg.solve(Q+self.eps*I, D) 121 | cd_filter_coeffs = np.concatenate([np.flipud(tmp[1:]),tmp]) 122 | elif self.method == 'least squares': # Eghbali et al. (2014) 123 | Q = np.ones([cd_filter_length, cd_filter_length]) 124 | for i in range(cd_filter_length): 125 | for j in range(cd_filter_length): 126 | if i != j: # diagonal entries are 1 127 | Q[i,j] = sin(pi*(i-j)*xi)/(pi*(i-j)*xi) 128 | Q = xi*Q 129 | 130 | nn = np.arange(-delay, delay+1) 131 | v = xi*self.int_aux(nn, KK, xi) 132 | 133 | cd_filter_coeffs = linalg.solve(Q+self.eps*np.eye(cd_filter_length), v) 134 | elif self.method == 'LS-CO': # Sheikh et al. (2016) 135 | Q1 = np.ones([cd_filter_length, cd_filter_length]) 136 | Q2 = np.ones([cd_filter_length, cd_filter_length]) 137 | for i in range(cd_filter_length): 138 | for j in range(cd_filter_length): 139 | if i != j: # diagonal entries are 1 140 | Q1[i,j] = sin(pi*(i-j)*xi)/(pi*(i-j)*xi) 141 | if xi < 1: 142 | Q2[i,j] = sin(pi*(i-j)*xi)/(pi*(i-j)*(xi-1)) 143 | else: 144 | Q2[i,j] = 0.0 145 | 146 | nn = np.arange(-delay, delay+1)[np.newaxis].T 147 | v = self.int_aux(nn, KK, xi) 148 | 149 | hopt = lambda l: linalg.solve(Q1+l*Q2, v) 150 | fun2 = lambda l: out_of_band_gain(hopt(np.abs(l)))-self.max_out_of_band_gain 151 | 152 | lambda_opt = np.abs(optimize.fsolve(fun2, 1e-10)) 153 | #if fun2(0.0) > 0: 154 | # lambda_opt = np.abs(optimize.fsolve(fun2, 0.0)) 155 | #else: 156 | # lambda_opt = 0.0 157 | #print(lambda_opt) 158 | 159 | cd_filter_coeffs = hopt(lambda_opt) 160 | elif self.method == 'maximally flat': # experimental 161 | Q = np.zeros([delay+1, delay+1], dtype=np.float64) 162 | Q[0,0] = 1 163 | for n in range(1,delay+1): 164 | Q[0,n] = 2 165 | for k in range(1,delay+1): 166 | for n in range(delay+1): 167 | Q[k,n] = 2*n**(2*k) 168 | if k%2 != 0: # odd rows 169 | Q[k,n] = -Q[k,n] 170 | 171 | w = sym.symbols('w') 172 | 173 | v = np.zeros([delay+1], dtype=np.complex128) 174 | for k in range(delay+1): 175 | v[k] = (sym.diff(sym.exp(sym.I*KK*w**2), w, 2*k)).subs(w,0) 176 | 177 | cd_filter_coeffs = linalg.solve(Q, v) 178 | cd_filter_coeffs = np.concatenate([np.flipud(cd_filter_coeffs[1::]), cd_filter_coeffs]) 179 | else: 180 | raise ValueError("wrong cd filter method") 181 | 182 | eps_o = out_of_band_gain(cd_filter_coeffs) 183 | E = inband_error(cd_filter_coeffs) 184 | #print(E) 185 | h = cd_filter_coeffs 186 | #(np.abs(np.matmul(A,h)-des)**2)) 187 | #np.set_printoptions(threshold=np.inf) 188 | tmp = (np.abs(np.matmul(A,h)-des)**2) 189 | #print(A.shape) 190 | #print(h.shape) 191 | #print(des.shape) 192 | #print((np.matmul(A,h)-des).shape) 193 | 194 | for i in range(delay): 195 | absdiff = abs(cd_filter_coeffs[i] - cd_filter_coeffs[cd_filter_length-i-1]) 196 | if(absdiff > 1e-2): 197 | warnings.warn("filter coefficients are not symmetric: absolute difference = {}".format(absdiff)) 198 | 199 | return cd_filter_coeffs.reshape([cd_filter_length]) 200 | 201 | def int_aux(self, n, KK, x): 202 | # ( integrate exp(j*KK*w^2)exp(j*n*w) dw=-x*pi...x*pi ) / (2*pi*x) 203 | # 204 | # eq. (13) in Eghbali et al. (2014), with fixed typo 205 | # +0j to avoid NANs with sqrt(negative number) 206 | # D = exp(-1j*(nn**2/(4*KK) + 3*pi/4))/(4*sqrt(pi*KK+0j)) \ 207 | # *(special.erf(exp(1j*3*pi/4)*(2*(Omega2/pi)*KK*pi-nn)/(2*sqrt(KK+0j))) \ 208 | # + special.erf(exp(1j*3*pi/4)*(2*(Omega2/pi)*KK*pi+nn)/(2*sqrt(KK+0j)))) 209 | # 210 | Omega1 = -pi*x 211 | Omega2 = pi*x 212 | y = exp(-1j*(n**2/(4*KK)+3*pi/4))/(2*(Omega2-Omega1))*sqrt(pi/KK+0j) \ 213 | *(special.erf(exp(-1j*pi/4)*(2*Omega1*KK+n)/(2*sqrt(KK+0j))) \ 214 | - special.erf(exp(-1j*pi/4)*(2*Omega2*KK+n)/(2*sqrt(KK+0j)))) 215 | return y 216 | --------------------------------------------------------------------------------