├── .gitignore ├── Cuboid.m ├── DelayFilter.m ├── Junction.m ├── LICENSE ├── Microphone.m ├── Position.m ├── PropLine.m ├── README.md ├── Room.m ├── Shape.m ├── Signal.m ├── Simulation.m ├── Source.m ├── private ├── getReflectPos.m ├── getReflectPosFace1.m ├── getReflectPosNObj.m └── getReflectPosOneDim.m ├── sdn_example.m └── sdn_tests.m /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *~ 3 | -------------------------------------------------------------------------------- /Cuboid.m: -------------------------------------------------------------------------------- 1 | %CUBOID Class defining the shape cuboid 2 | % This simple class defines the dimensions of a cuboid. 3 | % 4 | % See also Shape 5 | % 6 | % Copyright (c) 2010, Enzo De Sena 7 | classdef Cuboid < Shape 8 | properties 9 | x % length 10 | y % width 11 | z % height 12 | end 13 | 14 | methods 15 | function this = Cuboid(x, y, z) 16 | this.x = x; 17 | this.y = y; 18 | this.z = z; 19 | end 20 | end 21 | 22 | end 23 | 24 | -------------------------------------------------------------------------------- /DelayFilter.m: -------------------------------------------------------------------------------- 1 | classdef DelayFilter < handle 2 | %DELAY Class implementing a delay line 3 | % 4 | % Copyright (c) 2010, Enzo De Sena 5 | 6 | properties 7 | state 8 | index = 0; 9 | latency; 10 | end 11 | 12 | methods 13 | function this = DelayFilter(latency) 14 | this.state = zeros(1,latency+1); 15 | this.latency = latency; 16 | end 17 | 18 | function out = nextSample(this, thisSample) 19 | this.state(mod(this.index, this.latency+1) + 1) = thisSample; 20 | out = this.state(mod(this.index + 1, this.latency + 1) + 1); 21 | 22 | this.index = this.index + 1; 23 | end 24 | end 25 | 26 | end 27 | 28 | -------------------------------------------------------------------------------- /Junction.m: -------------------------------------------------------------------------------- 1 | %JUNCTION 2 | % 3 | % See also Position, Room, Shape 4 | % 5 | % Copyright (c) 2010, Enzo De Sena 6 | classdef Junction < handle 7 | properties 8 | wallFilters 9 | wallAttenuation = 1; 10 | position 11 | end 12 | 13 | properties (Access='private') 14 | propLinesIn = []; 15 | propLinesOut = []; 16 | end 17 | 18 | methods 19 | function this = Junction() 20 | end 21 | 22 | function [framesOut, pressureOut,framesIn] = getFramesOut(this, sourceFrame) 23 | % To produce the output to one of the neighbouring junctions, I 24 | % need all the inputs ready. 25 | 26 | assert(length(this.propLinesOut) == length(this.propLinesIn)); 27 | M = length(this.propLinesOut); 28 | 29 | framesIn = cell(1,M); 30 | for i=1:M 31 | framesIn{i} = this.propLinesIn{i}.getCurrentFrame(); 32 | end 33 | 34 | N = length(framesIn{1}); 35 | 36 | framesOut = cell(1,M); 37 | for i=1:M 38 | propLineOut = this.propLinesOut{i}; 39 | 40 | tempFrame = zeros(1, N); 41 | for j=1:M 42 | frameIn = framesIn{j}; % This is P+_j 43 | 44 | if nargin == 2 45 | frameIn = frameIn + sourceFrame ./ 2; 46 | end 47 | 48 | propLineIn = this.propLinesIn{j}; 49 | % Implementing S=1/M-I... 50 | if (propLineOut.getJunctionB() == propLineIn.getJunctionA()) 51 | a = 2/M - 1; 52 | else 53 | a = 2/M; 54 | end 55 | 56 | tempFrame = tempFrame + frameIn.*a; 57 | end 58 | 59 | 60 | %%% Filter the signal at the output 61 | filteredFrame = filter(this.wallFilters{i}, tempFrame) .* this.wallAttenuation; 62 | 63 | framesOut{i} = filteredFrame; 64 | 65 | if i == 1 66 | pressureOut = (2./M).*filteredFrame; 67 | else 68 | pressureOut = pressureOut + (2./M).*filteredFrame; 69 | end 70 | 71 | 72 | end 73 | end 74 | 75 | function pushNextFrameInPropLines(this, framesOut) 76 | M = length(this.propLinesOut); 77 | assert(length(framesOut) == M); 78 | for i=1:M 79 | this.propLinesOut{i}.setNextFrame(framesOut{i}); 80 | end 81 | end 82 | 83 | function addPropLineIn(this,propLine) 84 | this.propLinesIn{length(this.propLinesIn)+1} = propLine; 85 | end 86 | 87 | function addPropLineOut(this,propLine) 88 | this.propLinesOut{length(this.propLinesOut)+1} = propLine; 89 | end 90 | 91 | function n = getNumPropLinesOut(this) 92 | n = length(this.propLinesOut); 93 | end 94 | 95 | function setWallFilter(this, filters) 96 | this.wallFilters = filters; 97 | for i=1:this.getNumPropLinesOut() 98 | this.wallFilters{i}.PersistentMemory = true; 99 | end 100 | end 101 | end 102 | 103 | end 104 | 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 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 Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /Microphone.m: -------------------------------------------------------------------------------- 1 | classdef Microphone < handle 2 | %MICROPHONE Class of a microphone 3 | % 4 | % Copyright (c) 2010, Enzo De Sena 5 | 6 | properties 7 | position 8 | directivity 9 | heading 10 | end 11 | 12 | methods 13 | function set.directivity(this, directivity) 14 | assert(isvector(directivity)); 15 | 16 | % We store data only as row vectors 17 | [~, N] = size(directivity); 18 | assert(isvector(directivity)); 19 | if N > 1 20 | this.directivity = directivity; 21 | else 22 | this.directivity = directivity'; 23 | end 24 | end 25 | 26 | function set.heading(this, angle) 27 | this.heading = mod(angle, 2*pi); 28 | end 29 | 30 | function Gamma = getGamma(this, theta) 31 | Gamma = this.directivity * ... 32 | (cos(theta-this.heading).^(0:(length(this.directivity)-1))'); 33 | end 34 | 35 | function sig = getSignalFromSource(this, source) 36 | % 1,1 in the following call means: apply delay and attenuation. 37 | signalAt = source.getSignalAt(this.position, 1, 1); 38 | 39 | if length(this.directivity) ~= 1 || this.directivity ~= 1 40 | theta = Position.getAngle(this.position, source.position); 41 | Gamma = this.getGamma(theta); 42 | data = signalAt.data; 43 | 44 | sig = Signal(data .* Gamma, signalAt.FS, signalAt.initDelayTap); 45 | else 46 | sig = Signal(data, signalAt.FS, signalAt.initDelayTap); 47 | end 48 | end 49 | 50 | function sig = getSignalFromPosition(this, signal, position) 51 | % 1,1 in the following call means: apply delay and attenuation. 52 | signalAt = source.getSignalAt(this.position, 1, 1); 53 | 54 | if length(this.directivity) ~= 1 || this.directivity ~= 1 55 | theta = Position.getAngle(this.position, source.position); 56 | Gamma = this.getGamma(theta); 57 | data = signalAt.data; 58 | 59 | sig = Signal(data .* Gamma, signalAt.FS, signalAt.initDelayTap); 60 | else 61 | sig = Signal(data, signalAt.FS, signalAt.initDelayTap); 62 | end 63 | end 64 | end 65 | 66 | end 67 | 68 | -------------------------------------------------------------------------------- /Position.m: -------------------------------------------------------------------------------- 1 | %POSITION Position object 2 | % This object contains the cartesian coordinates of a point x,y,z. It 3 | % also contains some useful static methods (not all of them support the z 4 | % coordinate). 5 | % 6 | % Copyright (c) 2010, Enzo De Sena 7 | classdef Position < handle 8 | properties 9 | % We leave the properties undefined such that, if the coordinates 10 | % are not defined somewhere in the code, an error is generated 11 | x 12 | y 13 | z 14 | end 15 | 16 | methods 17 | function this = Position(x, y, z) 18 | if nargin >= 1 19 | this.x = double(x); 20 | end 21 | if nargin >= 2 22 | this.y = double(y); 23 | end 24 | if nargin == 3 25 | this.z = double(z); 26 | end 27 | end 28 | 29 | function r = r(this) 30 | % TODO: handle z 31 | r = sqrt(this.x.^2 + this.y.^2); 32 | end 33 | 34 | function theta = theta(this) 35 | % TODO: handle z 36 | theta = angle(this.x + 1i.*this.y); 37 | end 38 | 39 | function empty = isEmpty(this) 40 | empty = isempty(this.x) || isempty(this.y); 41 | end 42 | 43 | function equal = isEqual(this, toThis, tol) 44 | assert(isa(toThis,'Position')); 45 | 46 | if nargin <= 2 47 | tol = 10^(-10); 48 | end 49 | 50 | equal = (abs(this.x-toThis.x) < tol & abs(this.y-toThis.y) < tol & abs(this.z-toThis.z) < tol); 51 | end 52 | end 53 | 54 | methods (Static) 55 | function distance = getDistance(position1, position2) 56 | % TODO: handle the case where z is not defined 57 | distance = sqrt((position1.x - position2.x).^2 + (position1.y - position2.y).^2 + (position1.z - position2.z).^2); 58 | end 59 | 60 | function distance = distance(position1, position2) 61 | distance = Position.getDistance(position1, position2); 62 | end 63 | 64 | function ang = getAngle(position1, position2) 65 | % Get the anlge on a xy cartesian system where position1 is the 66 | % center of the cartesian system. 67 | % TODO: handle z 68 | 69 | ang = angle((position2.x - position1.x) + ... 70 | 1i.*(position2.y - position1.y)); 71 | end 72 | end 73 | end 74 | 75 | -------------------------------------------------------------------------------- /PropLine.m: -------------------------------------------------------------------------------- 1 | %PROPLINE 2 | % 3 | % See also Position, Room, Shape 4 | % 5 | % Copyright (c) 2010, Enzo De Sena 6 | classdef PropLine < handle 7 | properties 8 | attenuation 9 | end 10 | 11 | properties (Access='private') 12 | delayFilter 13 | nextFrame 14 | 15 | junctionA 16 | junctionB 17 | end 18 | 19 | properties (Constant=true) 20 | c = 343; 21 | end 22 | 23 | 24 | methods 25 | function this = PropLine(junctionA, junctionB, FS, offset) 26 | distance = Position.distance(junctionA.position, junctionB.position); 27 | 28 | delay = distance / this.c; 29 | delaySamples = round(delay .* FS); 30 | if nargin == 4 31 | delaySamples = delaySamples + offset; 32 | end 33 | %this.delayFilter = dfilt.delay(delaySamples); 34 | %this.delayFilter.PersistentMemory = true; 35 | this.delayFilter = DelayFilter(delaySamples); 36 | this.attenuation = (this.c./FS) ./ (distance); 37 | 38 | this.junctionA = junctionA; 39 | this.junctionB = junctionB; 40 | end 41 | 42 | function setNextFrame(this, frame) 43 | this.nextFrame = frame; 44 | end 45 | 46 | function out = getCurrentFrame(this) 47 | assert(~isempty(this.nextFrame)); 48 | %out = filter(this.delayFilter, this.nextFrame) .* this.attenuation; 49 | out = this.delayFilter.nextSample(this.nextFrame) .* this.attenuation; 50 | end 51 | 52 | 53 | %function updateDistance(this,distance, FS) 54 | % TODO 55 | %end 56 | 57 | function junct = getJunctionA(this) 58 | junct = this.junctionA; 59 | end 60 | 61 | function junct = getJunctionB(this) 62 | junct = this.junctionB; 63 | end 64 | end 65 | 66 | end 67 | 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scattering Delay Network (SDN) 2 | 3 | This is a Matlab implementation of the room acoustic simulator "Scattering Delay Network" (SDN) as described in [1] and [2]. If you use this code in your research publication, please make sure to cite [1]. 4 | 5 | Please notice that this Matlab implementation was written quickly in 2010 for the purpose of showing results in [2], and is **extremely slow**, possibly because of the sample-by-sample operation and Matlab's inefficient handling of object-oriented programming. **The algorithm itself is actually orders of magnitude faster than even fft-based convolution. A dynamic C++ implementation exists which uses << 1% of a single core on a modern CPU.** 6 | 7 | To get acquainted with the code, you can look into the script 'sdn_examples.m'. You can also check that everything is working as expected by running 'sdn_tests.m'. 8 | 9 | Please notice that the SDN algorithm is protected by USPTO patent 8,908,875 [3]. More specifically, the part of package that implements the patent is contained in the file "Simulation.m". **If you'd like to use this software for any reason other than non-commercial research purposes, please contact enzodesena AT gmail DOT com** 10 | 11 | 12 | 13 | ## Getting Started 14 | 15 | ### Prerequisites 16 | 17 | You need the Signal Processing Toolbox. 18 | 19 | ### Installation 20 | 21 | No need to install: just clone/download and you are ready to go! 22 | 23 | ### Example 24 | 25 | ```matlab 26 | % Example script generating the SDN output for: 27 | % - a 5x5x5 m room, 28 | % - a source positioned at (0.3,0.5,0.9) 29 | % - an omnidirectional microphone positioned at (0.4,0.1,0.4) 30 | % - wall reflection coefficient of 0.9 followed by a low-pass butterworth 31 | % filter 32 | % - sampling frequency: 44100 33 | % - calculating 10000 samples (i.e. ~0.23 sec) 34 | 35 | 36 | room = Room(); 37 | room.shape = Cuboid(5,5,5); 38 | d=fdesign.lowpass('N,F3dB',5,15000,44100); 39 | for i=1:6 40 | room.wallAttenuations{i} = 0.9; 41 | for j=1:5 42 | room.wallFilters{i}{j} = design(d,'butter'); %dfilt.delay(0);% 43 | end 44 | end 45 | 46 | source = Source(); 47 | source.position = Position(0.3,0.5,0.9); 48 | source.signal = Signal([1, zeros(1,9999)], 44100); 49 | 50 | microphone = Microphone(); 51 | microphone.position = Position(0.4,0.1,0.4); 52 | 53 | sim = Simulation(); 54 | sim.room = room; 55 | sim.source = source; 56 | sim.microphone = microphone; 57 | sim.NSamples = 10000; 58 | 59 | sim.frameLength = 1; 60 | 61 | output = sim.run(); 62 | 63 | plot(output) 64 | ``` 65 | 66 | 67 | ### Running the tests 68 | 69 | Run `sdn_tests`. 70 | 71 | 72 | ## Authors 73 | 74 | The conceptual work to develop SDN was carried out by all authors in [1] and [2]. 75 | 76 | The Matlab software in this repository was written by Enzo De Sena while he was a PhD student at King's college London in 2010. 77 | 78 | * **Enzo De Sena** - [desena.org](https://desena.org) 79 | 80 | ## References 81 | 82 | [1] E. De Sena, H. Hacıhabiboğlu, Z. Cvetković, and J. O. Smith III "Efficient Synthesis of Room Acoustics via Scattering Delay Networks," IEEE/ACM Trans. Audio, Speech and Language Process., vol. 23, no. 9, pp 1478 - 1492, Sept. 2015. 83 | 84 | [2] E. De Sena, H. Hacıhabiboğlu, and Z. Cvetković, "Scattering Delay Network: An Interactive Reverberator for Computer Games," in Proc. 41st AES International Conference: Audio for Games, London, UK, February 2011. 85 | 86 | [3] E. De Sena, H. Hacıhabiboğlu, and Z. Cvetković, "Electronic Device with Digital Reverberator and Method", US Patent n. 8,908,875, filed 2/2/2012, granted 09/12/2014. 87 | 88 | 89 | ## License 90 | 91 | This project is licensed under the AGPL License - see the [LICENSE](LICENSE) file for details. Also notice that parts of the code are protected under USPTO patent n. 8,976,977. 92 | -------------------------------------------------------------------------------- /Room.m: -------------------------------------------------------------------------------- 1 | %ROOM Room object 2 | % This object contains the properties of the simulated room. 3 | % 4 | % Copyright (c) 2010, Enzo De Sena 5 | classdef Room < handle 6 | properties 7 | shape 8 | wallFilters = []; 9 | wallAttenuations = []; 10 | end 11 | 12 | methods 13 | end 14 | 15 | end 16 | -------------------------------------------------------------------------------- /Shape.m: -------------------------------------------------------------------------------- 1 | %SHAPE Shape (Abstract) 2 | % 3 | % Copyright (c) 2010, Enzo De Sena 4 | classdef Shape 5 | properties 6 | end 7 | 8 | methods 9 | end 10 | 11 | end 12 | 13 | -------------------------------------------------------------------------------- /Signal.m: -------------------------------------------------------------------------------- 1 | classdef Signal < handle 2 | %SIGNAL The signal thisect. 3 | % To improve performance and lower memory usage, 4 | % instead of storing the time axis as a variable, 5 | % only the sampling frequency and the number of initial 6 | % zeros are kept. A signal that has non-zero values 7 | % in the negative part of the time axis has a negative 8 | % initial delay. When initDelay = 0, the data has its 9 | % first tap in zero. 10 | % Copyright (c) 2010, Enzo De Sena 11 | 12 | properties 13 | data 14 | FS = 44100; 15 | initDelayTap = 0; 16 | end 17 | 18 | 19 | methods 20 | function obj = Signal(data, FS, initDelayTap) 21 | if nargin >= 1 22 | obj.data = data; 23 | end 24 | if nargin >= 2 25 | assert(isscalar(FS)); 26 | obj.FS = FS; 27 | end 28 | if nargin >= 3 29 | obj.initDelayTap = initDelayTap; 30 | end 31 | end 32 | 33 | function sampleFreq = SP(this) 34 | sampleFreq = 1 / this.FS; 35 | end 36 | 37 | function delay = initDelay(this) 38 | delay = this.initDelayTap * this.SP; 39 | end 40 | 41 | function nTap = nTap(this) 42 | nTap = length(this.data); 43 | end 44 | 45 | function duration = duration(this) 46 | duration = this.nTap / this.FS; 47 | end 48 | 49 | function time = time(this) 50 | time = this.initDelay:(this.SP):(this.initDelay + this.duration - this.SP); 51 | end 52 | 53 | function newSignal = getHalfWavedSignal(this) 54 | newSignal = Signal(this.data .* (this.data > 0), this.FS, this.initDelayTap); 55 | end 56 | 57 | function newSignal = getSignalWithoutDC(this) 58 | newSignal = Signal(this.data - mean(this.data), this.FS, this.initDelayTap); 59 | end 60 | 61 | function newSignal = getSignalMultipliedBy(this, normalisation) 62 | newSignal = Signal(this.data .* normalisation, this.FS, this.initDelayTap); 63 | end 64 | 65 | function newSignal = getClippedSignal(this, thresholdUp, thresholdDown) 66 | newSignal = Signal((this.data < thresholdUp) .* this.data ... 67 | + (this.data >= thresholdUp) .* thresholdUp, this.FS, this.initDelayTap); 68 | 69 | if nargin >= 3 70 | newSignal.data = (newSignal.data > thresholdDown) .* newSignal.data ... 71 | + (newSignal.data <= thresholdDown) .* thresholdDown; 72 | end 73 | end 74 | 75 | function newSignal = getSignalElevatedTo(this, x) 76 | newSignal = Signal(this.data .^ x, this.FS, this.initDelayTap); 77 | end 78 | 79 | function newSignal = getFilteredSignal(this, num, den) 80 | assert(nargin == 2 | nargin == 3); 81 | if nargin == 3 82 | newSignal = Signal(filter(num,den,this.data), this.FS, this.initDelayTap); 83 | else 84 | % This is the case where num is the filter object 85 | newSignal = Signal(filter(num,this.data), this.FS, this.initDelayTap); 86 | end 87 | end 88 | 89 | function newSignal = getUpsampledSignal(this, N) 90 | newSignal = Signal(upsample(this.data,N), N .* this.FS, N .* this.initDelayTap); 91 | end 92 | 93 | function rms = RMS(this) 94 | rms = sqrt(mean(this.data.^2)); 95 | end 96 | 97 | function set.data(this, data) 98 | 99 | % We store data only as row vectors 100 | [M, N] = size(data); 101 | assert(isempty(data) || isvector(data)); 102 | if N > 1 && M == 1 103 | this.data = data; 104 | else 105 | this.data = data'; 106 | end 107 | end 108 | 109 | function set.initDelayTap(this, initDelayTap) 110 | this.initDelayTap = initDelayTap; 111 | end 112 | 113 | function value = get.data(this) 114 | if rem(this.initDelayTap, 1) == 0 115 | value = this.data; 116 | else 117 | fracDelay = rem(this.initDelayTap, 1); 118 | [N, D] = Signal.thiran(fracDelay); 119 | value = filter(N, D, this.data); 120 | end 121 | end 122 | 123 | function plot(this) 124 | plot(this.time, this.data); 125 | end 126 | 127 | function frame = getFrame(this, frameID, frameLength) 128 | % This function gives back a frame of the signal. 129 | % frameID=1...inf. 130 | 131 | firstIndex = (frameID-1).*frameLength + 1; 132 | lastIndex = firstIndex + frameLength - 1; 133 | frame = Signal(this.data((firstIndex):(min(lastIndex, this.nTap))),this.FS); 134 | end 135 | 136 | 137 | function obj = clone(this) 138 | obj = Signal(this.data, this.FS, this.initDelayTap); 139 | end 140 | end 141 | 142 | methods (Static) 143 | 144 | function sumSignal = getSum(signal1, signal2) 145 | if isempty(signal1) || isempty(signal1.data) 146 | sumSignal = signal2.clone(); 147 | return 148 | end 149 | if isempty(signal2) || isempty(signal2.data) 150 | sumSignal = signal1.clone(); 151 | return 152 | end 153 | 154 | assert(signal1.FS == signal2.FS); 155 | 156 | if (signal1.initDelayTap == signal2.initDelayTap && signal1.nTap == signal2.nTap) 157 | sumSignal = Signal(signal1.data + signal2.data, signal1.FS, signal1.initDelayTap); 158 | return 159 | end 160 | 161 | sumSignal = Signal; 162 | sumSignal.FS = signal1.FS; 163 | sumSignal.initDelayTap = ... 164 | min(signal1.initDelayTap, signal2.initDelayTap); 165 | 166 | nx = signal1.time; 167 | ny = signal2.time; 168 | nz = min(min(nx),min(ny)) : (sumSignal.SP) : max(max(nx),max(ny)); 169 | z1 = zeros(1,length(nz)); 170 | z2 = z1; 171 | z1(find((nz>=(min(nx) - sumSignal.SP / 2)) & ... 172 | (nz<=(max(nx) + sumSignal.SP / 2)))) = signal1.data; %#ok 173 | z2(find((nz>=(min(ny) - sumSignal.SP / 2)) & ... 174 | (nz<=(max(ny) + sumSignal.SP / 2)))) = signal2.data; %#ok 175 | sumSignal.data = z1 + z2; 176 | end 177 | 178 | function xcor = getXCorr(signal1, signal2) 179 | assert(signal1.FS == signal2.FS); 180 | xcor = Signal; 181 | xcor.FS = signal1.FS; 182 | 183 | tapDiff = signal2.initDelayTap - signal1.initDelayTap; 184 | if tapDiff >= 0 185 | data1 = signal1.data; 186 | data2 = [zeros(1, tapDiff), signal2.data]; 187 | else 188 | data1 = [zeros(1, -tapDiff), signal1.data]; 189 | data2 = signal2.data; 190 | end 191 | xcor.data = xcorr(data1, data2); 192 | maxNTap = max(length(data1), length(data2)); 193 | xcor.initDelayTap = -(maxNTap - 1); 194 | end 195 | 196 | function flipped = getFlipped(signal) 197 | flipped = Signal(fliplr(signal.data), signal.FS, - (signal.initDelayTap + signal.nTap)); 198 | end 199 | 200 | function convo = getConvolution(signal1, signal2) 201 | assert(signal1.FS == signal2.FS); 202 | convo = Signal; 203 | convo.FS = signal1.FS; 204 | 205 | N1 = signal1.nTap; 206 | N2 = signal2.nTap; 207 | if N1*N2 < (N1+N2-1)*(3*log2(N1+N2-1)+1) 208 | convo.data = conv(signal1.data, signal2.data); 209 | else 210 | display('Running convolution in the freq domain'); 211 | X1 = fft(signal1.data,N1+N2-1); 212 | X2 = fft(signal2.data,N1+N2-1); 213 | Y = X1.*X2; 214 | if (isreal(signal1.data) && isreal(signal2.data)) 215 | convo.data = real(ifft(Y)); 216 | else 217 | convo.data = ifft(Y); 218 | end 219 | end 220 | convo.initDelayTap = signal1.initDelayTap + signal2.initDelayTap; 221 | end 222 | 223 | function pinkified = pinkify(signal) 224 | poles = [0.9986823 0.9914651 0.9580812 0.8090598 0.2896591]; 225 | zeros = [0.9963594 0.9808756 0.9097290 0.6128445 -0.0324723]; 226 | 227 | N = poly(zeros); 228 | D = poly(poles); 229 | pinkified = Signal(filter(N,D,signal.data), signal.FS, signal.initDelay); 230 | end 231 | 232 | function matrix = getMatrix(signals, withInitialDelay) 233 | % This function returns a matrix containing the signals on the 234 | % columns. 235 | nSignals = length(signals); 236 | lengths = zeros(1, nSignals); 237 | initDelayTaps = zeros(1, nSignals); 238 | 239 | for i = 1:nSignals 240 | signal_i = signals(i); 241 | lengths(i) = signal_i.nTap; 242 | initDelayTaps(i) = signal_i.initDelayTap; 243 | end 244 | 245 | maxLenght = max(lengths + initDelayTaps); 246 | matrix = zeros(maxLenght, nSignals); 247 | 248 | for i = 1:nSignals 249 | signal_i = signals(i); 250 | matrix(:, i) = [zeros(initDelayTaps(i), 1); signal_i.data'; ... 251 | zeros(maxLenght - (initDelayTaps(i) + lengths(i)), 1)]; 252 | end 253 | 254 | if ~withInitialDelay 255 | minInitDelayTap = min(initDelayTaps); 256 | matrix = matrix((minInitDelayTap + 1):maxLenght, :); 257 | end 258 | end 259 | 260 | end 261 | 262 | methods (Access = private, Static = true) 263 | % Author: Huseyin Hacihabiboglu 264 | function [N, D] = thiran(fdelay, order) 265 | n = 0:order; 266 | a = zeros(1, order); 267 | for i=1:order 268 | a(i)=(-1)^i*((factorial(order)/(factorial(i)*factorial(order-i))))*prod(((fdelay-order+n)./(fdelay-order+i+n)),2); 269 | end 270 | 271 | D = [1 a]; 272 | N = fliplr(D); 273 | end 274 | end 275 | end 276 | 277 | -------------------------------------------------------------------------------- /Simulation.m: -------------------------------------------------------------------------------- 1 | %SIMULATION Class implementing the Scattering Delay Network (SDN) algorithm 2 | % 3 | % See also Position, Room, Shape 4 | % 5 | % Author: Enzo De Sena 6 | % 7 | % Copyright (c) 2010, Enzo De Sena, UK 8 | % All rights reserved. 9 | % 10 | % Please notice that this algorithm is protected by USPTO patent: 11 | % E. De Sena, H. Hac?habibo?lu, and Z. Cvetkovi?, inventors; 12 | % King's College London, assignee, "Electronic Device with Digital 13 | % Reverberator and Method", US Patent n. 8,908,875, filed 2/2/2012, 14 | % granted 09/12/2014. 15 | % For any queries, please contact Enzo De Sena at enzodesena AT gmail DOT com 16 | classdef Simulation < handle 17 | properties 18 | room 19 | source 20 | microphone 21 | frameLength = 1; 22 | NSamples 23 | end 24 | 25 | methods 26 | function output = run(this, verbose) 27 | if nargin < 2 28 | verbose = true; 29 | end 30 | 31 | %%% Initialize variables 32 | 33 | M = 6; % 3D case 34 | FS = this.source.signal.FS; 35 | 36 | % junctions is the vector containing all the junction 37 | % objects. 38 | junctions = cell(1,M); 39 | for i=1:M 40 | junction = Junction(); 41 | 42 | %%% Run the ray-tracing module 43 | junction.position = getReflectPos(this.room, i, this.source.position, this.microphone.position); 44 | junctions{i} = junction; 45 | end 46 | 47 | 48 | 49 | for i=1:M 50 | for j=1:M 51 | if i==j 52 | continue; 53 | end 54 | 55 | % The offset -1 means that we want a propagation line 56 | % with delaysample - 1. This is due to the way the 57 | % updating at the junction is made, which intrinsecally 58 | % delays the output by one sample. 59 | propLine = PropLine(junctions{i}, junctions{j}, FS, -1); 60 | 61 | % Initialize the propagation lines with empty frames 62 | propLine.setNextFrame(zeros(1,this.frameLength)); 63 | 64 | % Tell the in and out junctions that this is their 65 | % propagation line. 66 | junctions{i}.addPropLineOut(propLine); 67 | junctions{j}.addPropLineIn(propLine); 68 | 69 | propLine.attenuation = 1; 70 | end 71 | end 72 | 73 | % TBD: define the filters 74 | for i=1:M 75 | junctions{i}.wallAttenuation = this.room.wallAttenuations{i}; %this.room.wallAttenuations{i}; 76 | junctions{i}.setWallFilter(this.room.wallFilters{i}); 77 | end 78 | 79 | 80 | 81 | % Define source propagation line 82 | sPropLines = cell(1,M); 83 | sDummyJunction = Junction(); 84 | sDummyJunction.position = this.source.position; 85 | firstSourceFrame = this.source.signal.getFrame(1, this.frameLength).data; 86 | for i=1:M 87 | propLine = PropLine(sDummyJunction, junctions{i}, FS); 88 | propLine.setNextFrame(firstSourceFrame); 89 | sPropLines{i} = propLine; 90 | end 91 | 92 | % Define microphone propagation line 93 | mPropLines = cell(1,M); 94 | mDummyJunction = Junction(); 95 | mDummyJunction.position = this.microphone.position; 96 | for i=1:M 97 | mPropLine = PropLine(junctions{i}, mDummyJunction, FS); 98 | 99 | distSourceJunct = Position.distance(junctions{i}.position, sDummyJunction.position); 100 | distJunctMic = Position.distance(junctions{i}.position, mDummyJunction.position); 101 | 102 | 103 | % We use this attenuation in such a way that the first 104 | % order reflections have an exact attenuation. (We don't 105 | % have (mPropLine.c ./ FS) at the numerator, because it is 106 | % already at the numerator of the attenuation of the 107 | % propLine between source and junction. 108 | mPropLine.attenuation = 1 / (1 + distJunctMic/distSourceJunct); 109 | mPropLines{i} = mPropLine; 110 | end 111 | 112 | % Define one last propagation line between source and 113 | % microphone. This line models the LOS component. 114 | smPropLine = PropLine(sDummyJunction, mDummyJunction, FS); 115 | smPropLine.setNextFrame(firstSourceFrame); 116 | 117 | %%% Run the simulation sample by sample 118 | tempOutput = zeros(M, this.NSamples); 119 | output = zeros(1, this.NSamples); 120 | n = 1; 121 | while true 122 | if n > this.NSamples 123 | break; 124 | end 125 | 126 | if verbose && mod(n/100,1) == 0 127 | display(['>> Running frame n. ', num2str(n)]); 128 | end 129 | 130 | framesOut = cell(1,M); 131 | for i=1:M 132 | 133 | [framesOut{i}, pressureFrame] = junctions{i}.getFramesOut(sPropLines{i}.getCurrentFrame()); 134 | 135 | mPropLines{i}.setNextFrame(pressureFrame); 136 | 137 | tempOutput(i,n) = mPropLines{i}.getCurrentFrame(); 138 | end 139 | 140 | %TODO: weight the mic directivity pattern 141 | output(n) = sum(tempOutput(:,n)) + smPropLine.getCurrentFrame(); 142 | 143 | smPropLine.setNextFrame(this.source.signal.getFrame(n+1, this.frameLength).data); 144 | 145 | for i=1:M 146 | sPropLines{i}.setNextFrame(this.source.signal.getFrame(n+1, this.frameLength).data); 147 | junctions{i}.pushNextFrameInPropLines(framesOut{i}); 148 | end 149 | 150 | 151 | n = n + 1; 152 | end 153 | 154 | end 155 | end 156 | 157 | methods(Access='private') 158 | 159 | end 160 | end 161 | 162 | -------------------------------------------------------------------------------- /Source.m: -------------------------------------------------------------------------------- 1 | classdef Source < handle 2 | %SOURCE Source object 3 | % 4 | % Copyright (c) 2010, Enzo De Sena 5 | 6 | properties 7 | position = Position; 8 | 9 | % heading is the angle formed between the x-axis and the source's 10 | % axis. Angles are anticlockwise. 11 | heading = 0; 12 | 13 | % directivity = 1 means omnidirectional 14 | directivity = 1; 15 | 16 | signal 17 | end 18 | 19 | properties (Constant) 20 | c = 343; 21 | end 22 | 23 | methods 24 | function set.directivity(this, directivity) 25 | assert(isvector(directivity)); 26 | 27 | % We store data only as row vectors 28 | [~, N] = size(directivity); 29 | assert(isvector(directivity)); 30 | if N > 1 31 | this.directivity = directivity; 32 | else 33 | this.directivity = directivity'; 34 | end 35 | end 36 | 37 | function set.heading(this, angle) 38 | this.heading = mod(angle, 2*pi); 39 | end 40 | 41 | function Gamma = getGamma(this, theta) 42 | Gamma = abs(this.directivity * ... 43 | cos(theta-this.heading).^(0:(length(this.directivity)-1))'); 44 | end 45 | 46 | function sig = getSignalAt(this, atPosition, applyAttenuation, applyDelay) 47 | if nargin <= 2 48 | applyAttenuation = 1; 49 | applyDelay = 1; 50 | end 51 | if nargin <= 3 52 | applyDelay = 1; 53 | end 54 | 55 | % The distance between point and loudspeaker 56 | distance = Position.getDistance(atPosition, this.position); 57 | 58 | % The angle by witch the loudspeaker sees the position 59 | alfa = Position.getAngle(this.position, atPosition); 60 | 61 | Gamma = this.getGamma(alfa); 62 | data = Gamma .* this.signal.data; 63 | 64 | if applyDelay 65 | delay = distance / this.c; 66 | delayTap = round(delay * this.signal.FS); 67 | else 68 | delayTap = 0; 69 | end 70 | 71 | if applyAttenuation == 1; 72 | data = data / distance; 73 | end 74 | 75 | sig = Signal(data, this.signal.FS, this.signal.initDelayTap + delayTap); 76 | end 77 | end 78 | 79 | end 80 | 81 | -------------------------------------------------------------------------------- /private/getReflectPos.m: -------------------------------------------------------------------------------- 1 | %GETREFLECTPOS Outputs the position of a reflection in a cube 2 | % This function gives back the position in cartesian coordinates of the 3 | % reflection on the face of a cube for a source in a certain position, 4 | % and observed at a certain point. 5 | % The output is an instance of Position in cartesian coordinates. 6 | % 7 | % POS = GETREFLECTPOS(ROOM, FACEINDEX, SOURCEPOS, OBSERVPOS) 8 | % 9 | % FACEINDEX is equal to 1 for the reflection on the y=0 face of the 10 | % cube; FACEINDEX is 2 for the plane x = ROOM.shape.x etc.. FACEINDEX is 11 | % 5 for the face at the top of the cube (z = ROOM.shape.z). 12 | % 13 | % When the source and observation point are on the same plane, the 14 | % function outputs a position with some NaNs. 15 | % 16 | % See also Position, Room, Shape 17 | % 18 | % Copyright (c) 2010, Enzo De Sena 19 | function pos = getReflectPos(room, faceIndex, sourcePos, observPos) 20 | assert(isa(room, 'Room'), 'Room is not an istance of the class Room'); 21 | assert(isa(room.shape, 'Cuboid'), 'This function has been implemented only for cuboid rooms'); 22 | assert(room.shape.x > 0 & room.shape.y > 0 & room.shape.z > 0, 'Invalid room dimensions'); 23 | assert(isa(sourcePos, 'Position'), 'sourcePos is not an istance of the class Position'); 24 | assert(isa(observPos, 'Position'), 'observPos is not an istance of the class Position'); 25 | assert(sourcePos.x >= 0 & sourcePos.y >= 0 & sourcePos.z >= 0, 'Invalid source position'); 26 | assert(sourcePos.x <= room.shape.x & sourcePos.y <= room.shape.y & sourcePos.z <= room.shape.z, 'Source is outside the room'); 27 | assert(observPos.x <= room.shape.x & observPos.y <= room.shape.y & observPos.z <= room.shape.z, 'Observation point is outside the room'); 28 | assert(observPos.x >= 0 & observPos.y >= 0 & observPos.z >= 0, 'Invalid observation position'); 29 | assert((mod(faceIndex,1)==0)& faceIndex >=1 & faceIndex <= 6, 'Invalid face index'); 30 | 31 | 32 | % Convert the problem with face index given by faceIndex to the single 33 | % and simpler case of faceIndex=1 34 | 35 | switch faceIndex 36 | case 1 37 | pos = getReflectPosFace1(sourcePos, observPos); 38 | case 2 39 | % x'=y, y'=xr-x, z'=z 40 | sourcePosT = Position(sourcePos.y, room.shape.x-sourcePos.x, sourcePos.z); 41 | observPosT = Position(observPos.y, room.shape.x-observPos.x, observPos.z); 42 | posT = getReflectPosFace1(sourcePosT, observPosT); 43 | 44 | % y=x',x=xr-y', z=z' 45 | pos = Position(room.shape.x-posT.y, posT.x, posT.z); 46 | case 3 47 | % x'=xr-x,y'=yr-y,z'=z 48 | sourcePosT = Position(room.shape.x-sourcePos.x, room.shape.y-sourcePos.y, sourcePos.z); 49 | observPosT = Position(room.shape.x-observPos.x, room.shape.y-observPos.y, observPos.z); 50 | posT = getReflectPosFace1(sourcePosT, observPosT); 51 | 52 | % x=xr-x',y=yr-y',z=z' 53 | pos = Position(room.shape.x-posT.x, room.shape.y-posT.y, posT.z); 54 | case 4 55 | % x'=yr-y, y'=x, z'=z 56 | sourcePosT = Position(room.shape.y-sourcePos.y, sourcePos.x, sourcePos.z); 57 | observPosT = Position(room.shape.y-observPos.y, observPos.x, observPos.z); 58 | posT = getReflectPosFace1(sourcePosT, observPosT); 59 | 60 | % y=yr-x', x=y', z=z' 61 | pos = Position(posT.y, room.shape.y-posT.x, posT.z); 62 | case 5 63 | % x'=x, y'=zr-z, z'=y 64 | sourcePosT = Position(sourcePos.x, room.shape.z-sourcePos.z, sourcePos.y); 65 | observPosT = Position(observPos.x, room.shape.z-observPos.z, observPos.y); 66 | posT = getReflectPosFace1(sourcePosT, observPosT); 67 | 68 | % x=x',z=zr-y', y=z' 69 | pos = Position(posT.x, posT.z, room.shape.z-posT.y); 70 | case 6 71 | % x'=x, y'=z, z'=yr-y 72 | sourcePosT = Position(sourcePos.x, sourcePos.z, room.shape.y-sourcePos.y); 73 | observPosT = Position(observPos.x, observPos.z, room.shape.y-observPos.y); 74 | posT = getReflectPosFace1(sourcePosT, observPosT); 75 | 76 | % x=x', z=y', y=yr-z' 77 | pos = Position(posT.x, room.shape.y-posT.z, posT.y); 78 | otherwise 79 | assert(false) 80 | end 81 | 82 | debug = true; 83 | if debug 84 | switch faceIndex 85 | case 1 86 | assert(pos.y == 0); 87 | assert((pos.x >= sourcePos.x & pos.x <= observPos.x) | (pos.x <= sourcePos.x & pos.x >= observPos.x)); 88 | assert((pos.z >= sourcePos.z & pos.z <= observPos.z) | (pos.z <= sourcePos.z & pos.z >= observPos.z)); 89 | case 2 90 | assert(pos.x == room.shape.x); 91 | assert((pos.y >= sourcePos.y & pos.y <= observPos.y) | (pos.y <= sourcePos.y & pos.y >= observPos.y)); 92 | assert((pos.z >= sourcePos.z & pos.z <= observPos.z) | (pos.z <= sourcePos.z & pos.z >= observPos.z)); 93 | case 3 94 | assert(pos.y == room.shape.y); 95 | assert((pos.x >= sourcePos.x & pos.x <= observPos.x) | (pos.x <= sourcePos.x & pos.x >= observPos.x)); 96 | assert((pos.z >= sourcePos.z & pos.z <= observPos.z) | (pos.z <= sourcePos.z & pos.z >= observPos.z)); 97 | case 4 98 | assert(pos.x == 0); 99 | assert((pos.y >= sourcePos.y & pos.y <= observPos.y) | (pos.y <= sourcePos.y & pos.y >= observPos.y)); 100 | assert((pos.z >= sourcePos.z & pos.z <= observPos.z) | (pos.z <= sourcePos.z & pos.z >= observPos.z)); 101 | case 5 102 | assert(pos.z == room.shape.z); 103 | assert((pos.x >= sourcePos.x & pos.x <= observPos.x) | (pos.x <= sourcePos.x & pos.x >= observPos.x)); 104 | assert((pos.y >= sourcePos.y & pos.y <= observPos.y) | (pos.y <= sourcePos.y & pos.y >= observPos.y)); 105 | case 6 106 | assert(pos.z == 0); 107 | assert((pos.x >= sourcePos.x & pos.x <= observPos.x) | (pos.x <= sourcePos.x & pos.x >= observPos.x)); 108 | assert((pos.y >= sourcePos.y & pos.y <= observPos.y) | (pos.y <= sourcePos.y & pos.y >= observPos.y)); 109 | end 110 | end 111 | end 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /private/getReflectPosFace1.m: -------------------------------------------------------------------------------- 1 | % Copyright (c) 2010, Enzo De Sena 2 | function pos = getReflectPosFace1(sourcePos, observPos) 3 | % Solve the same problem, but only for the surface 1 4 | 5 | pos = Position; 6 | pos.y = double(0); 7 | 8 | % For the following conversions, see Enzo's notes. 9 | pos.x = getReflectPosOneDim(sourcePos.x, sourcePos.y, observPos.x, observPos.y); 10 | pos.z = getReflectPosOneDim(sourcePos.z, sourcePos.y, observPos.z, observPos.y); 11 | end 12 | -------------------------------------------------------------------------------- /private/getReflectPosNObj.m: -------------------------------------------------------------------------------- 1 | %GETREFLECTPOSNOBJ The same as GETREFLECTPOS but with non-object as inputs 2 | % This function serves as a interface to GETREFLECTPOS, if you don't want 3 | % to define objects. 4 | % 5 | % See also getReflectPos 6 | % 7 | % Copyright (c) 2010, Enzo De Sena 8 | 9 | function [x,y,z] = getReflectPosNObj(xRoom, yRoom, zRoom, faceIndex, xSource, ySource, zSource, xObserv, yObserv, zObserv) 10 | room = Room(); 11 | room.shape = Cuboid(xRoom, yRoom, zRoom); 12 | sourcePos = Position(xSource, ySource, zSource); 13 | observPos = Position(xObserv, yObserv, zObserv); 14 | 15 | pos = getReflectPos(room, faceIndex, sourcePos, observPos); 16 | 17 | x = pos.x; 18 | y = pos.y; 19 | z = pos.z; 20 | end 21 | -------------------------------------------------------------------------------- /private/getReflectPosOneDim.m: -------------------------------------------------------------------------------- 1 | % Copyright (c) 2010, Enzo De Sena 2 | function x = getReflectPosOneDim(x1,y1,x2,y2) 3 | if x1 == x2 4 | x = x1; 5 | else 6 | x = (x1.*y2 + x2.*y1)/(y1+y2); 7 | end 8 | end 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sdn_example.m: -------------------------------------------------------------------------------- 1 | % Example script generating the SDN output for: 2 | % - a 5x5x5 m room, 3 | % - a source positioned at (0.3,0.5,0.9) 4 | % - an omnidirectional microphone positioned at (0.4,0.1,0.4) 5 | % - wall reflection coefficient of 0.9 followed by a low-pass butterworth 6 | % filter 7 | % - sampling frequency: 44100 8 | % - calculating 10000 samples (i.e. ~0.23 sec) 9 | 10 | 11 | room = Room(); 12 | room.shape = Cuboid(5,5,5); 13 | d=fdesign.lowpass('N,F3dB',5,15000,44100); 14 | for i=1:6 15 | room.wallAttenuations{i} = 0.9; 16 | for j=1:5 17 | room.wallFilters{i}{j} = design(d,'butter'); %dfilt.delay(0);% 18 | end 19 | end 20 | 21 | source = Source(); 22 | source.position = Position(0.3,0.5,0.9); 23 | source.signal = Signal([1, zeros(1,9999)], 44100); 24 | 25 | microphone = Microphone(); 26 | microphone.position = Position(0.4,0.1,0.4); 27 | 28 | sim = Simulation(); 29 | sim.room = room; 30 | sim.source = source; 31 | sim.microphone = microphone; 32 | sim.NSamples = 10000; 33 | 34 | sim.frameLength = 1; 35 | 36 | output = sim.run(); 37 | 38 | plot(output) -------------------------------------------------------------------------------- /sdn_tests.m: -------------------------------------------------------------------------------- 1 | %TESTS 2 | % This routine contains the test for the SDN Matlab implmenetation 3 | % 4 | % Copyright (c) 2010, Enzo De Sena 5 | 6 | 7 | 8 | %% First obvious test case 9 | 10 | [x,y,z] = getReflectPosNObj(1,1,1,1,0.5,0.5,0.5,0.5,0.5,0.5); 11 | assert(sum([x,y,z]==[0.5,0.0,0.5])==3); 12 | 13 | [x,y,z] = getReflectPosNObj(1,1,1,2,0.5,0.5,0.5,0.5,0.5,0.5); 14 | assert(sum([x,y,z]==[1,0.5,0.5])==3); 15 | 16 | [x,y,z] = getReflectPosNObj(1,1,1,3,0.5,0.5,0.5,0.5,0.5,0.5); 17 | assert(sum([x,y,z]==[0.5,1,0.5])==3); 18 | 19 | [x,y,z] = getReflectPosNObj(1,1,1,4,0.5,0.5,0.5,0.5,0.5,0.5); 20 | assert(sum([x,y,z]==[0,0.5,0.5])==3); 21 | 22 | [x,y,z] = getReflectPosNObj(1,1,1,5,0.5,0.5,0.5,0.5,0.5,0.5); 23 | assert(sum([x,y,z]==[0.5,0.5,1])==3); 24 | 25 | [x,y,z] = getReflectPosNObj(1,1,1,6,0.5,0.5,0.5,0.5,0.5,0.5); 26 | assert(sum([x,y,z]==[0.5,0.5,0])==3); 27 | 28 | %% 29 | 30 | [x,y,z] = getReflectPosNObj(1,1,1,1,0.25,0.5,0.5,0.75,0.5,0.5); 31 | assert(sum([x,y,z]==[0.5,0,0.5])==3); 32 | 33 | [x,y,z] = getReflectPosNObj(1,1,1,2,0.25,0.5,0.5,0.75,0.5,0.5); 34 | assert(sum([x,y,z]==[1,0.5,0.5])==3); 35 | 36 | [x,y,z] = getReflectPosNObj(1,1,1,3,0.25,0.5,0.5,0.75,0.5,0.5); 37 | assert(sum([x,y,z]==[0.5,1,0.5])==3); 38 | 39 | [x,y,z] = getReflectPosNObj(1,1,1,4,0.25,0.5,0.5,0.75,0.5,0.5); 40 | assert(sum([x,y,z]==[0,0.5,0.5])==3); 41 | 42 | [x,y,z] = getReflectPosNObj(1,1,1,5,0.25,0.5,0.5,0.75,0.5,0.5); 43 | assert(sum([x,y,z]==[0.5,0.5,1])==3); 44 | 45 | [x,y,z] = getReflectPosNObj(1,1,1,6,0.25,0.5,0.5,0.75,0.5,0.5); 46 | assert(sum([x,y,z]==[0.5,0.5,0])==3); 47 | 48 | 49 | %% 50 | 51 | delayFilter = DelayFilter(1); 52 | assert(delayFilter.nextSample(0)==0); 53 | assert(delayFilter.nextSample(1)==0); 54 | assert(delayFilter.nextSample(2)==1); 55 | assert(delayFilter.nextSample(3)==2); 56 | assert(delayFilter.nextSample(4)==3); 57 | assert(delayFilter.nextSample(0)==4); 58 | assert(delayFilter.nextSample(4)==0); 59 | 60 | delayFilter = DelayFilter(2); 61 | assert(delayFilter.nextSample(0)==0); 62 | assert(delayFilter.nextSample(1)==0); 63 | assert(delayFilter.nextSample(2)==0); 64 | assert(delayFilter.nextSample(3)==1); 65 | assert(delayFilter.nextSample(4)==2); 66 | assert(delayFilter.nextSample(0)==3); 67 | assert(delayFilter.nextSample(4)==4); 68 | 69 | %% Testing with object oriented implementation 70 | 71 | room = Room(); 72 | room.shape = Cuboid(1,1,1); 73 | sourcePos = Position(0,0,0); 74 | observPos = Position(0,1,1); 75 | 76 | res = getReflectPos(room,1,sourcePos,observPos); 77 | assert(res.isEqual(Position(0,0,0))); 78 | 79 | %% 80 | 81 | room = Room(); 82 | room.shape = Cuboid(1,1,1); 83 | sourcePos = Position(0,0,0); 84 | observPos = Position(1,0,1); 85 | 86 | res = getReflectPos(room,2,sourcePos,observPos); 87 | assert(res.isEqual(Position(1,0,1))); 88 | 89 | res = getReflectPos(room,3,sourcePos,observPos); 90 | assert(res.isEqual(Position(0.5,1,0.5))); 91 | 92 | res = getReflectPos(room,4,sourcePos,observPos); 93 | assert(res.isEqual(Position(0,0,0))); 94 | 95 | res = getReflectPos(room,5,sourcePos,observPos); 96 | assert(res.isEqual(Position(1,0,1))); 97 | 98 | res = getReflectPos(room,6,sourcePos,observPos); 99 | assert(res.isEqual(Position(0,0,0))); 100 | 101 | %% 102 | 103 | room = Room(); 104 | room.shape = Cuboid(1,1,1); 105 | sourcePos = Position(0.2,0.2,0.2); 106 | observPos = Position(0.8,0.8,0.8); 107 | 108 | res = getReflectPos(room,1,sourcePos,observPos); 109 | assert(res.isEqual(Position(0.32,0,0.32))); 110 | 111 | res = getReflectPos(room,2,sourcePos,observPos); 112 | assert(res.isEqual(Position(1,1-0.32,1-0.32))); 113 | 114 | res = getReflectPos(room,3,sourcePos,observPos); 115 | assert(res.isEqual(Position(1-0.32,1,1-0.32))); 116 | 117 | res = getReflectPos(room,4,sourcePos,observPos); 118 | assert(res.isEqual(Position(0,0.32,0.32))); 119 | 120 | res = getReflectPos(room,5,sourcePos,observPos); 121 | assert(res.isEqual(Position(1-0.32,1-0.32,1))); 122 | 123 | res = getReflectPos(room,6,sourcePos,observPos); 124 | assert(res.isEqual(Position(0.32,0.32,0))); 125 | 126 | %% Testing the objects 127 | 128 | junctionA = Junction(); 129 | junctionA.position = Position(0,0,0); 130 | junctionB = Junction(); 131 | junctionB.position = Position(0.02,0,0); 132 | 133 | 134 | distance = Position.distance(junctionA.position, junctionB.position); 135 | assert(distance==.02); 136 | 137 | c = 343; 138 | FS = 44100; 139 | attenuation = (c./FS)/(distance); 140 | delay = distance / c; 141 | latency = round(delay.*FS); 142 | assert(latency == 3); 143 | 144 | propLine = PropLine(junctionA, junctionB, FS); 145 | assert(propLine.getJunctionA() == junctionA); 146 | assert(propLine.getJunctionB() == junctionB); 147 | 148 | 149 | propLine.setNextFrame(1); 150 | assert(propLine.getCurrentFrame() == 0); 151 | propLine.setNextFrame(2); 152 | assert(propLine.getCurrentFrame() == 0); 153 | propLine.setNextFrame(3); 154 | assert(propLine.getCurrentFrame() == 0); 155 | propLine.setNextFrame(-1); 156 | assert(propLine.getCurrentFrame() == 1*attenuation); 157 | propLine.setNextFrame(-1); 158 | assert(propLine.getCurrentFrame() == 2*attenuation); 159 | propLine.setNextFrame(-1); 160 | assert(propLine.getCurrentFrame() == 3*attenuation); 161 | propLine.setNextFrame(-1); 162 | assert(propLine.getCurrentFrame() == -1*attenuation); 163 | 164 | 165 | 166 | %% 167 | 168 | FS = 44100; 169 | c = 343; 170 | 171 | minDist = c/FS; 172 | room = Room(); 173 | room.shape = Cuboid(8*minDist,8*minDist,1000); 174 | %room.shape = Cuboid(8*minDist,1000,1000); 175 | for i=1:6 176 | room.wallAttenuations{i} = 1; 177 | for j=1:5 178 | room.wallFilters{i}{j} = dfilt.delay(0); 179 | end 180 | end 181 | 182 | source = Source(); 183 | source.position = Position(6*minDist,5*minDist,500); 184 | %source.position = Position(6*minDist,1000-3*minDist,500); 185 | source.signal = Signal([1, zeros(1,8820)], 44100); 186 | 187 | microphone = Microphone(); 188 | microphone.position = Position(3*minDist,5*minDist,500); 189 | %microphone.position = Position(3*minDist,1000-3*minDist,500); 190 | 191 | sim = Simulation(); 192 | sim.room = room; 193 | sim.source = source; 194 | sim.microphone = microphone; 195 | sim.NSamples = 18; 196 | 197 | sim.frameLength = 1; 198 | 199 | output = sim.run(false); 200 | 201 | cmp = zeros(1,18); 202 | cmp(3+1) = 1/3; 203 | cmp(7+1) = 1/7; 204 | cmp(9+1) = 1/9; 205 | cmp(10+1) = 1/(2*sqrt(5^2+1.5^2)); 206 | cmp(6+1) = 1/(2*sqrt(3^2+1.5^2)); 207 | cmp(11+1) = 2/(15*sqrt(3^2+1.5^2)); 208 | cmp(10+1) = cmp(10+1)+1/20; 209 | cmp(13+1) = 1/15; 210 | cmp(15+1) = 2/(15*sqrt(5^2+1.5^2)); 211 | cmp(13+1) = cmp(13+1)+1/20; 212 | cmp(16+1) = 2/(5*7*sqrt(1.5^2+5^2)); 213 | cmp(16+1) = cmp(16+1)+1/(10*sqrt(1.5^2+5^2)); 214 | cmp(16+1) = cmp(16+1)+1/(10*sqrt(1.5^2+3^2)); 215 | cmp(13+1) = cmp(13+1)+2/(5*7*sqrt(1.5^2+3^2)); 216 | cmp(14+1) = 1/60; 217 | cmp(17+1) = cmp(17+1)+1/(5*7)*(-3/5); 218 | cmp(16+1) = cmp(16+1)+1/(10*sqrt(1.5^2+3^2))*(-3/5); 219 | cmp(16+1) = cmp(16+1)+1/(10*sqrt(1.5^2+3^2))*(-3/5); 220 | cmp(15+1) = cmp(15+1)+2/75; 221 | 222 | assert(abs(sum(output-cmp))<10^(-10)); 223 | 224 | 225 | %% Done! 226 | display('All tests succeded'); clear all; 227 | 228 | 229 | --------------------------------------------------------------------------------