API Reference
DiagramBuilder
DiagramBuilder
A helper class for incrementally building signal processing diagrams using Matplotlib.
This class provides high-level methods to add standard diagram components like blocks, arrows, combiners, and input/output labels, keeping track of layout and threading.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_length
|
float
|
Default horizontal size of blocks. |
1.0
|
block_height
|
float
|
Default vertical size of blocks. |
1.0
|
fontsize
|
int
|
Default font size for all text. |
20
|
Returns:
Type | Description |
---|---|
DiagramBuilder
|
created object. |
Examples:
>>> from signalblocks import DiagramBuilder
>>> db1 = DiagramBuilder()
>>> db2 = DiagramBuilder(block_length=2, fontsize=16)
Source code in signalblocks\DiagramBuilder.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
|
__add_element_position__(input_pos, output_pos, feedback_pos)
Inner method. Adds a new element with the given input, output and feedback positions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_pos
|
NDArray or list
|
Input position of the block. |
required |
output_pos
|
NDArray or list
|
Output position of the block. |
required |
feedback_pos
|
NDArray or list
|
Feedback port position of the block. |
required |
Source code in signalblocks\DiagramBuilder.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
__draw_angled_arrow__(initial_position, final_position, text=None, text_offset=0.2, arrow=True, fontsize=14, first_segment='horizontal', orientation='horizontal')
Inner method. Draws a right-angled arrow composed of two segments, with a specified first segment orientation and optional label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_position
|
NDarray or list
|
Coordinates of the starting point of the arrow. |
required |
final_position
|
NDarray or list
|
Coordinates of the ending point of the arrow. |
required |
text
|
string
|
Label to display in the block. |
None
|
text_offset
|
float
|
Vertical offset for the text position. |
0.2
|
arrow
|
bool
|
Indicates if it must finish or not in an arrow. |
True
|
fontsize
|
int
|
font size of the text inside the block. If not entered, default |
14
|
first_segment
|
string
|
Drawing order: {'horizontal', 'vertical'} |
'horizontal'
|
orientation
|
string or float
|
Direction of the block: {'horizontal', 'vertical', 'up', 'down', 'left', 'right', angle}. |
'horizontal'
|
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of output point of the arrow. |
Source code in signalblocks\DiagramBuilder.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
|
__draw_arrow__(initial_position, length, text=None, text_position='above', text_offset=0.2, arrow=True, fontsize=14, orientation='horizontal')
Inner method. Draws a horizontal arrow with optional label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_position
|
NDarray or list
|
Coordinates of the starting point of the arrow. |
required |
length
|
float
|
Horizontal length of the block. If not entered, default |
required |
text
|
string
|
Label to display in the block. |
None
|
text_position
|
string
|
Position of the optional text: {'before', 'after', 'above'} |
'above'
|
text_offset
|
float
|
Vertical offset for the text position. |
0.2
|
arrow
|
bool
|
Indicated if an line mush finish or not in an arrow. |
True
|
fontsize
|
int
|
font size of the text inside the block. If not entered, default |
14
|
orientation
|
string or float
|
Direction of the block: {'horizontal', 'vertical', 'up', 'down', 'left', 'right', angle}. |
'horizontal'
|
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of output point of the arrow. |
Source code in signalblocks\DiagramBuilder.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
__draw_block__(initial_position, text=None, text_below=None, text_above=None, text_offset=0.1, input_text=None, input_side=None, length=1.5, height=1, fontsize=14, linestyle='-', orientation='horizontal')
Inner method. Draws a rectangular block with centered text, optional texts below and/or above and optional input arrow with text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_position
|
NDarray or list
|
Coordinates of the center position of the input edge of the block. |
required |
text
|
string
|
Label to display in the block. |
None
|
text_below
|
string
|
Label to display below the block. |
None
|
text_above
|
string
|
Label to display above the block. |
None
|
text_offset
|
float
|
Vertical offset for the text position. |
0.1
|
input_text
|
string
|
Label for the optional input arrow (below or above the block). |
None
|
input_side
|
string
|
Side to place the input arrow: {'bottom', 'top', None} |
None
|
length
|
float
|
Horizontal length of the block. If not entered, default |
1.5
|
height
|
float
|
Vertical height of the block. If not entered, default |
1
|
fontsize
|
int
|
font size of the text inside the block. If not entered, default |
14
|
linestyle
|
string
|
linestyle of the block edge: {'-, '--, ':', '-.'}. |
'-'
|
orientation
|
string or float
|
Direction of the block: {'horizontal', 'vertical', 'up', 'down', 'left', 'right', angle}. |
'horizontal'
|
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of the center position of the output edge of the block. |
Source code in signalblocks\DiagramBuilder.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
__draw_combiner__(initial_position, height=1, input_text=None, input_side='bottom', operation='mult', text_offset=0.1, signs=[None, None], fontsize=14, orientation='horizontal')
Inner method. Draws a combiner block: a circle with a multiplication sign (×), sum sign (+) or substraction sign (-) inside, with optional signs on each input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_position
|
NDarray or list
|
Coordinates of the starting point of the arrow. |
required |
height
|
float
|
Vertical height of the block. If not entered, default |
1
|
input_text
|
string
|
Label for the input arrow (below or above the arrow). |
None
|
input_side
|
string
|
Side of the lateral input: {'bottom', 'top'}. |
'bottom'
|
operation
|
string
|
Operation of the combiner: {'mult', 'sum', 'dif'}. |
'mult'
|
text_offset
|
float
|
Vertical offset for the text position. |
0.1
|
signs
|
list
|
Sign to be shown on the horizontal (signs[0]) and vertical (signs[1]) inputs. |
[None, None]
|
fontsize
|
int
|
font size of the text inside the block. If not entered, default |
14
|
orientation
|
string or float
|
Direction of the block: {'horizontal', 'vertical', 'up', 'down', 'left', 'right', angle}. |
'horizontal'
|
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of output point of the combiner. |
Source code in signalblocks\DiagramBuilder.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
__draw_mult_combiner__(initial_position, length, inputs, operation='sum', orientation='horizontal')
Inner method. Draws a summation or multiplication block with multiple inputs distributed along the left edge of a circle, from pi/2 to 3*pi/2. Inputs can have a sign.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_position
|
NDarray or list
|
Coordinates of the starting point of the arrow. |
required |
length
|
float
|
Horizontal length of the block. If not entered, default |
required |
inputs
|
list of str
|
Thread names to combine. |
required |
operation
|
string
|
Operation of the combiner: {'mult', 'sum'}. |
'sum'
|
orientation
|
string or float
|
Direction of the block: {'horizontal', 'vertical', 'up', 'down', 'left', 'right', angle}. |
'horizontal'
|
Returns:
Type | Description |
---|---|
NDArray
|
Coordinates of output point of the combiner. |
Source code in signalblocks\DiagramBuilder.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
|
__draw_rotated_text__(anchor_point, text, angle, rotate_text=True, ha='center', va='center', fontsize=16, offset=(0, 0))
Inner method. Draws text rotated around the anchor point with optional offset. Text position: rotation(anchor_point + offset)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
anchor_point
|
NDArray or list
|
Coordinates of the anchor point. |
required |
text
|
string
|
String to display. LaTeX math accepted (without $...$). |
required |
angle
|
float
|
Rotation angle in degrees. |
required |
rotate_text
|
bool
|
Indicates if text must be rotated or not. |
True
|
ha
|
string
|
Horizontal alignment: {'center', 'left', 'right'}. |
'center'
|
va
|
string
|
Vertical alignment: {'center', 'bottom', 'top'}. |
'center'
|
fontsize
|
int
|
Font size. |
16
|
offset
|
NDArray or list
|
Coordinates of texr position respect to anchor point, before rotation. |
(0, 0)
|
Source code in signalblocks\DiagramBuilder.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
__get_rotated_pos__(init_pos, outvector, angle)
Inner method. Compute rotated point init_pos + outvector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_pos
|
NDArray or list
|
Initial position of the block (relative origin of coordinates). |
required |
outvector
|
NDArray or list
|
Output vector before rotation (relative position with respect to init_pos). |
required |
angle
|
float
|
Rotation angle in degrees. |
required |
Returns:
Type | Description |
---|---|
NDArray
|
Rotated position of vector init_pos + outvector. |
Source code in signalblocks\DiagramBuilder.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
__init__(block_length=1.0, block_height=1.0, fontsize=20)
(Private) Creator of the DiagramBuilder class.
Source code in signalblocks\DiagramBuilder.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
add(name, kind='block', thread='main', position=None, debug=False, **kwargs)
Adds an element to the block diagram at the current or specified position of a given thread.
This is the main interface for constructing diagrams by adding components such as blocks, arrows,
inputs, outputs, combiners, and connectors. The kind
parameter determines the type of element,
and each type accepts specific keyword arguments listed below.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Main label or identifier for the element. |
required |
kind
|
str
|
Type of element. One of: - 'block': Rectangular block. - 'arrow': Straight line with ending arrow. - 'angled_arrow': Rect angle line with or without ending arrow. - 'input': Arrow with text before it. - 'output': Arrow with text after it. - 'line': Straight line without arrow ending. - 'combiner': Circle with (x), (+) or (-) and additional input. - 'mult_combiner': Combiner with multiple inputs. |
'block'
|
thread
|
str
|
Thread identifier. |
'main'
|
position
|
tuple or str or None
|
(x, y) position, 'auto' (for mult_combiner), or None to use current thread position. |
None
|
debug
|
bool
|
If True, prints thread positions after placing the element. |
False
|
The **kwargs
vary depending on the kind
:
-
kind = 'block':
- text (str, optional): Label inside the block (defaults to
name
). - text_above (str, optional): Text above the block.
- text_below (str, optional): Text below the block.
- text_offset (float, optional): Offset for above/below text (defaults to 0.1).
- input_text (str, optional): Label for input arrow.
- input_side (str, optional): Side of a second optional input: {'top', 'bottom'} (defaults to
None
) - length (float, optional): Block length (defaults to
self.block_length
). - height (float, optional): Block height (defaults to
self.block_height
).. - linestyle (str, optional): Block border line style (defaults to
-
). - orientation (str or float, optional): Orientation of the block: {'horizontal', 'vertical', 'up', 'down' 'right', left' or angle in degrees} (defaults to 'horizontal').
- fontsize (int, optional): Text font size (defaults to
self.fontsize
).
- text (str, optional): Label inside the block (defaults to
-
kind = 'arrow', 'input', 'output' or 'line':
- text (str, optional): Text on the arrow or line (defaults to
name
). - text_position (str, optional): 'above', 'below', 'before', or 'after' (defaults to 'above' for 'arrow' and 'line', 'before' for 'input', and 'after' for 'output').
- text_offset (float, optional): Offset for text (defaults to 0.1).
- length (float, optional): Arrow or line length (defaults to
self.block_length
). - orientation (str or float, optional): Orientation of the block: {'horizontal', 'vertical', 'up', 'down' 'right', left' or angle in degrees} (defaults to 'horizontal').
- fontsize (int, optional): Text font size (defaults to
self.fontsize
).
- text (str, optional): Text on the arrow or line (defaults to
-
kind = 'angled_arrow':
- text (str, optional): Text on the arrow or line (defaults to
name
). - final_pos (Numpy.NDarray or list): Coordinates of the ending point of the arrow.
- text_position (str, optional): 'above', 'below', 'before', or 'after' (defaults to 'above' for 'arrow' and 'line', 'before' for 'input', and 'after' for 'output').
- text_offset (float, optional): Offset for text (defaults to 0.1).
- arrow (bool, optional): Indicates if it must finish or not in an arrow.
- first_segment (string, optional): Drawing order: {'horizontal', 'vertical'}
- orientation (str or float, optional): Orientation of the block: {'horizontal', 'vertical', 'up', 'down' 'right', left' or angle in degrees} (defaults to 'horizontal').
- fontsize (int, optional): Text font size (defaults to
self.fontsize
).
- text (str, optional): Text on the arrow or line (defaults to
-
kind = 'combiner':
- operation (string, optional): Operation of the combiner: {'mult', 'sum', 'dif'} (defaultds to 'mult').
- height (float, optional): Vertical height of the block. (defaults to
self.block_height
). - input_side (string, optional): Side of the lateral input: {'bottom', 'top'} (defaults to 'bottom').
- input_text (string, optional): Label for the input arrow (below or above the arrow).
- text_offset (float, optional): Offset for text (defaults to 0.1).
- signs (list, optional): Sign to be shown on the horizontal (signs[0]) and vertical (signs[1]) inputs.
- orientation (str or float, optional): Orientation of the block: {'horizontal', 'vertical', 'up', 'down' 'right', left' or angle in degrees} (defaults to 'horizontal').
- fontsize (int, optional): Text font size (defaults to
self.fontsize
).
-
kind = 'mult_combiner':
- operation (string, optional): Operation of the combiner: {'mult', 'sum', 'dif'} (defaults to 'mult').
- length (float, optional): Total element length (defaults to
self.block_length
). - inputs (list of str): Thread names to combine.
- operation (string, optional): Operation of the combiner: {'mult', 'sum'} (defaults to 'sum').
- orientation (str or float, optional): Orientation of the block: {'horizontal', 'vertical', 'up', 'down' 'right', left' or angle in degrees} (defaults to 'horizontal').
- fontsize (int, optional): Text font size (defaults to
self.fontsize
).
Examples:
>>> db = DiagramBuilder()
>>> db.add("x(t)", kind="input")
>>> db.add("H(s)", kind="block")
>>> db.add("y(t)", kind="output")
Source code in signalblocks\DiagramBuilder.py
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
|
get_current_element()
Returns the current element index (last added).
Returns:
Type | Description |
---|---|
int
|
Index of the last added element. |
Source code in signalblocks\DiagramBuilder.py
1045 1046 1047 1048 1049 1050 1051 1052 |
|
get_position(element=None)
Returns the positions of the specified element index. If no element specified, last added element is used.
The return is a dictinoary with coordinates of input_pos
, output_pos
and feedback_pos
(feedback port coordinates).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
element
|
int
|
index of the element. |
None
|
Returns:
Type | Description |
---|---|
dict of Tuples
|
Dictionary with three 2-element tuples: |
Source code in signalblocks\DiagramBuilder.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
|
get_thread_position(thread='main')
Returns the current output position of the specified thread.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread
|
str
|
Thread identifier. |
'main'
|
Source code in signalblocks\DiagramBuilder.py
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
|
print_threads()
Prints name of each thread in diagram and actual position.
Examples:
>>> from signalblocks import DiagramBuilder
>>> db = DiagramBuilder(block_length=1, fontsize=16)
>>> # Upper thread
>>> db.add("x_1(t)", kind="input", thread='upper', position=(0, 1))
>>> db.add("mult", kind="combiner", thread='upper', input_text="e^{-j\omega_0 t}", input_side='top', operation='mult')
>>> db.add("", kind="line", thread='upper')
>>> # Lower thread
>>> db.add("x_2(t)", kind="input", thread='lower', position=(0, -1))
>>> db.add("mult", kind="combiner", input_text="e^{j\omega_0 t}", input_side='bottom', operation='mult', thread='lower')
>>> db.add("", kind="line", thread='lower')
>>> input_threads = ['upper', 'lower']
>>> # Adder
>>> db.add("", kind="mult_combiner", inputs=input_threads, position="auto", operation='sum')
>>> # Rest of the diagram (main thread)
>>> db.add("x(t)", kind="output")
>>> db.show()
>>> db.print_threads()
Source code in signalblocks\DiagramBuilder.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
show(margin=0.5, scale=1.0, savepath=None)
Displays the current diagram or saves it to a file.
Adjusts the view to fit the full diagram with an optional margin and scaling factor. If no elements have been drawn, simply displays an empty figure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
margin
|
float
|
Margin to add around the diagram (in data units). |
0.5
|
scale
|
float
|
Scaling factor for the figure size. |
1.0
|
savepath
|
str
|
If provided, saves the figure to the specified path (e.g., 'diagram.png' or 'diagram.pdf'). If None, the diagram is shown in an interactive window. |
None
|
Source code in signalblocks\DiagramBuilder.py
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
|
SignalPlotter
SignalPlotter
A helper class to plot signals y a minimalistic way. It has predefined typical signals, like rect, tri, Heaviside, delta, sinc, ... It allow to do operations with signals, like time shifts and inversions, sums, products, convolutions, ...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr_str
|
str
|
A string expression defining the signal, e.g. "x(t)=sin(t)*u(t)". |
None
|
horiz_range
|
tuple
|
Tuple (t_min, t_max) specifying the horizontal plotting range. |
(-5, 5)
|
vert_range
|
tuple
|
Tuple (y_min, y_max) specifying the vertical range. Auto-scaled if None. |
None
|
period
|
float
|
If provided, the signal is treated as periodic with this period. |
None
|
num_points
|
int
|
Number of points used to discretize the time axis. |
1000
|
figsize
|
tuple
|
Size of the figure in centimeters (width, height). |
(8, 3)
|
tick_size_px
|
int
|
Size of axis tick marks in pixels. |
5
|
xticks
|
list or auto or None
|
Positions of x-axis ticks. If 'auto', they are generated automatically. |
'auto'
|
yticks
|
list or auto or None
|
Same for y-axis. |
'auto'
|
xtick_labels
|
list of str
|
Labels for xticks. Must match xticks in length. |
None
|
ytick_labels
|
list of str
|
Labels for yticks. Must match yticks in length. |
None
|
pi_mode
|
bool
|
If True, x and y tick labels are shown as fractionary multiples of π if possible. |
False
|
fraction_ticks
|
bool
|
If True, tick labels are shown as rational fractions. |
False
|
xticks_delta
|
float
|
If provided, generates xticks at this interval (when xticks='auto'). |
None
|
yticks_delta
|
float
|
Same for yticks. |
None
|
save_path
|
str
|
If provided, saves the plot to the given path instead of displaying. |
None
|
show_plot
|
bool
|
Whether to show the plot window (if False and save_path is given, it only saves). |
True
|
color
|
str
|
Color for the plot line and impulses. |
'black'
|
alpha
|
float
|
Transparency for background label boxes (between 0 and 1). |
0.5
|
Examples:
>>> from signalblocks import SignalPlotter
>>> sp = SignalPlotter("x(t)=rect(t)", horiz_range=(-2, 2), pi_mode=True).plot()
>>> SignalPlotter("x(t)=delta(t/2-1) + 3*delta(t + 2)", color='blue', figsize=(8,4)).plot('x')
>>> signal1 = SignalPlotter("x(t)=cos(4 pi t)*tri(t/2)", alpha=0.7, horiz_range=[-3, 3], xticks=np.linspace(-2, 2, 9), color='blue', figsize=(12,4))
>>> signal1.plot()
>>> SignalPlotter("x(t)=pw((t**2, (t>-1) & (t<0)), (-t, (t>=0) & (t<1)), (0, True))", horiz_range=[-2.5, 2.5], xticks=np.linspace(-2, 2, 9), color='blue', period=2)
Source code in signalblocks\SignalPlotter.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 |
|
__init__(expr_str=None, horiz_range=(-5, 5), vert_range=None, period=None, num_points=1000, figsize=(8, 3), tick_size_px=5, xticks='auto', yticks='auto', xtick_labels=None, ytick_labels=None, xticks_delta=None, yticks_delta=None, pi_mode=False, fraction_ticks=False, save_path=None, show_plot=True, color='black', alpha=0.5)
(Private) Creator of the SignalPlotter class.
Source code in signalblocks\SignalPlotter.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
add_signal(expr_str, label=None, period=None)
Adds a new signal to the internal dictionary for later plotting.
- The expression is parsed symbolically using SymPy.
- If other signals are referenced, their definitions are recursively substituted.
- If period
is set, the signal will be expanded as a sum of time-shifted versions over the full horizontal range.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr_str
|
str
|
Signal definition in the form "name(var) = expression", e.g. "x(t) = rect(t) + delta(t-1)". The expression may include previously defined signals. |
required |
label
|
str
|
Custom label for the vertical axis when plotting this signal. |
None
|
period
|
float
|
If provided, the signal will be treated as periodic with this period. |
None
|
Examples:
>>> sp = SignalPlotter(horiz_range=(-1, 1), fraction_ticks=True, figsize=(12, 4))
>>> sp.add_signal("x1(t) = tri(t)")
>>> sp.add_signal("x2(t) = delta(t)", period=0.2)
>>> sp.add_signal("x3(t) = x1(t) * (1 + x2(t))", label="x_3(t)")
>>> sp.plot("x3")
>>> sp.add_signal("x4(t) = exp((-2+j*8*pi)*t)*u(t)")
>>> sp.add_signal("x5(t) = re(x4(t))", label="\Re\{x_4(t)\}")
>>> sp.plot('x5')
Source code in signalblocks\SignalPlotter.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
draw_function(horiz_range=None)
Plots the continuous part of the signal over the specified horizontal range.
This method is typically called after setup_axes()
.
This method is usually called internally from plot()
, but can also be used manually.
This method:
- Evaluates the function defined in self.func
across self.t_vals
.
- Plots the result as a smooth curve using the configured color and linewidth.
- Automatically detects and adds ellipsis ("⋯") on the left/right ends if:
- The signal is marked as periodic, or
- Significant energy exists just outside the plotting range.
Notes:
- This method does not draw delta impulses. Use draw_impulses()
for that.
- Ellipsis are drawn at 5% beyond the plot edges when appropriate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
horiz_range
|
tuple
|
Tuple (t_min, t_max) to override the default horizontal range. If None, uses |
None
|
Examples:
>>> self.draw_function()
>>> self.draw_impulses() # to add deltas on top of the curve
Source code in signalblocks\SignalPlotter.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
|
draw_impulses()
Draws Dirac delta impulses at the extracted positions and amplitudes.
This method is typically called after draw_functions()
.
This method is usually called internally from plot()
, but can also be used manually.
This method:
- Iterates over the list of impulse locations (self.impulse_locs
)
and their corresponding amplitudes (self.impulse_areas
).
- Calls _draw_single_impulse()
for each impulse located within
the current horizontal plotting range (self.horiz_range
).
Notes:
- This method only draws impulses that are visible within the plotting window.
- Periodicity is not assumed. Use add_signal(..., period=...)
to manually expand periodic impulses.
- The drawing includes both a vertical arrow and a bold label showing the impulse area.
Examples:
>>> self.draw_function()
>>> self.draw_impulses()
Source code in signalblocks\SignalPlotter.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
|
draw_labels()
Adds axis labels to the x and y axes using the current signal variable and name.
This method is typically called after draw_ticks()
.
This method is usually called internally from plot()
, but can also be used manually.
This method:
- Retrieves the current axis limits.
- Places the x-axis label slightly to the right of the horizontal axis arrow.
- Places the y-axis label slightly below the top of the vertical axis arrow.
- Uses LaTeX formatting for the labels (e.g., "x(t)", "y(\tau)", etc.).
- The labels use the values of self.xlabel
and self.ylabel
.
Notes:
- This method is called automatically in plot()
after drawing ticks and arrows.
- Rotation for y-axis label is disabled to keep it horizontal.
Examples:
>>> self.draw_labels()
Source code in signalblocks\SignalPlotter.py
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
|
draw_ticks(tick_size_px=None, xticks=None, yticks=None, xtick_labels='auto', ytick_labels='auto')
Draws tick marks and labels on both x- and y-axes, using automatic or manual configurations.
This method is typically called after draw_impulses()
.
This method is usually called internally from plot()
, but can also be used manually.
Features:
- Adds tick marks and LaTeX-formatted labels.
- Integrates impulse positions into xticks automatically, unless explicitly overridden.
- Supports:
- pi_mode
: labels as multiples of π.
- fraction_ticks
: labels as rational fractions.
- Hiding y=0 label if x=0 tick is shown (to avoid overlapping at the origin).
- Avoids duplicate tick values based on numerical tolerance.
Notes:
- If xticks_delta
or yticks_delta
are provided (in constructor), evenly spaced ticks are placed at multiples.
- If custom labels are passed and their count does not match the tick list, raises ValueError.
- Labels are drawn on white rounded boxes for visibility over plots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tick_size_px
|
int
|
Length of tick marks in pixels. If None, uses self.tick_size_px. |
None
|
xticks
|
list or auto or None
|
X-axis tick positions. - 'auto': automatically computed ticks (even spacing or using xticks_delta). - list: manually specified tick positions. - None: ticks are shown only at Dirac impulse positions. |
None
|
yticks
|
list or auto or None
|
Y-axis tick positions.
- Same behavior as |
None
|
xtick_labels
|
list or auto or None
|
Custom labels for xticks (must match length of xticks). |
'auto'
|
ytick_labels
|
list or auto or None
|
Same for yticks. |
'auto'
|
Examples:
>>> self.draw_ticks(xticks='auto', yticks=[-1, 0, 1], ytick_labels=['-1', '0', '1'])
Source code in signalblocks\SignalPlotter.py
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
|
plot(name=None)
Plots the signal specified by name (or the default if defined via expr_str in constructor).
This method:
- Initializes the expression from expr_str_pending
(if any), only once.
- Looks up the signal in the internal dictionary (self.signal_defs
) using its name.
- Sets up symbolic and numeric representations of the signal.
- Removes DiracDelta terms from the continuous part.
- Extracts impulses and prepares data for plotting.
- Calls the full sequence: axis setup, function drawing, impulses, ticks, labels, and final display/save.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the signal to plot, e.g., "x1". If None and |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the signal is not defined or its variable cannot be determined. |
Examples:
>>> SignalPlotter("x(t)=rect(t-1)").plot()
>>> sp1 = SignalPlotter("x(t)=rect(t-1)", period=2)
>>> sp1.plot("x")
>>> sp2 = SignalPlotter()
>>> sp2.add_signal("x(t) = rect(t)")
>>> sp2.plot("x")
Source code in signalblocks\SignalPlotter.py
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 |
|
plot_convolution_result(x_name, h_name, num_points=None, show_expr=False)
Computes and plots the convolution result y(t) = (x * h)(t) between two signals x(t) and h(t).
This method automatically: - Detects if either x(τ) or h(t−τ) consists only of Dirac deltas, and applies the convolution property for impulses. - Otherwise, performs numerical integration over τ for a given range of t values. - Displays the resulting function y(t), including impulses if present.
Notes: - Impulse responses are handled symbolically, while general functions are integrated numerically. - Uses scipy.integrate.quad for general convolution integrals.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_name
|
str
|
Name of the signal x(t), previously defined via |
required |
h_name
|
str
|
Name of the signal h(t), previously defined via |
required |
num_points
|
int
|
Number of time samples to compute for numerical integration. Defaults to self.num_points. |
None
|
show_expr
|
bool
|
Reserved for future use; currently unused. |
False
|
Source code in signalblocks\SignalPlotter.py
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 |
|
plot_convolution_steps(x_name, h_name, t_val, tau=None, t=None)
Plots four key signals involved in a convolution step at a fixed time t_val
:
x(tau), x(t-tau), h(tau), h(t-tau)., all in terms of τ.
This method is particularly useful for visualizing the time-reversed and shifted versions of the input signals used in the convolution integral.
Notes: - The horizontal axis is adjusted for time-reversed signals (e.g., t−τ), and tick labels are shifted accordingly. - Four separate plots are generated in sequence, with labels and axes automatically set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_name
|
str
|
Name of the signal x, previously defined with |
required |
h_name
|
str
|
Name of the signal h, previously defined with |
required |
t_val
|
float
|
The fixed time t at which the convolution step is evaluated. |
required |
tau
|
Symbol or str
|
Symbol for the integration variable (default: 'tau'). |
None
|
t
|
Symbol or str
|
Symbol for the time variable (default: 't'). |
None
|
Examples:
>>> sp = SignalPlotter()
>>> sp.add_signal("x(t)=sinc(t)")
>>> sp.add_signal("h(t)=sinc(t/2)")
>>> sp.plot_convolution_steps("x", "h", t_val=1)
Source code in signalblocks\SignalPlotter.py
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 |
|
plot_convolution_view(expr_str, t_val, label=None, tau=None, t=None)
Plots an intermediate signal in the convolution process, such as x(t−τ), h(τ+t), etc.
This method:
- Substitutes the convolution variable t with a fixed value t_val
in a symbolic expression.
- Evaluates the resulting signal in terms of τ.
- Optionally adjusts x-axis direction and labels if the expression has a form like (t − τ) or (t + τ).
- Automatically handles periodic xtick reversal or shift based on convolution expression.
- Renders the function using existing plot methods (function, impulses, ticks, etc.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr_str
|
str
|
A symbolic expression involving τ and t, e.g. "x(t - tau)" or "h(t + tau)".
The base signal must already be defined with |
required |
t_val
|
float
|
The value of the time variable |
required |
label
|
str
|
Custom y-axis label to display (default is derived from the expression). |
None
|
tau
|
Symbol or str
|
Symbol to use as integration variable (default: 'tau'). |
None
|
t
|
Symbol or str
|
Symbol used in shifting (default: 't'). |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If the base signal is not defined or the expression format is invalid. |
Examples:
>>> sp = SignalPlotter(xticks=[-1, 0, 3], num_points=200, fraction_ticks=True)
>>> sp.add_signal("x(t)=exp(-2t)*u(t)")
>>> sp.add_signal("h(t)=u(t)")
>>> sp.plot_convolution_view("x(t - tau)", t_val=1)
>>> sp.plot_convolution_view("h(t + tau)", t_val=2, tau='lambda', t='omega')
Source code in signalblocks\SignalPlotter.py
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 |
|
setup_axes(horiz_range=None)
Configures the plot axes: hides borders, sets limits, and draws arrow-like axes.
This method is typically called after _prepare_plot()
to finalize the plot appearance.
This method is usually called internally from plot()
, but can also be used manually.
This method:
- Hides the default box (spines) around the plot.
- Clears all default ticks.
- Sets the horizontal and vertical limits based on the signal range.
- Adds margin space around the plotted data to improve visual clarity.
- Draws custom x- and y-axis arrows using annotate
.
- Calls tight_layout()
to prevent label clipping.
Notes:
- The horizontal axis includes a 20% margin on both sides.
- The vertical axis includes 30% below and 60% above the data range.
- The vertical range must be computed beforehand via _prepare_plot()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
horiz_range
|
tuple
|
If provided, overrides the default horizontal range. |
None
|
Examples:
>>> self._prepare_plot()
>>> self.setup_axes()
Source code in signalblocks\SignalPlotter.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 |
|
show()
Displays or saves the final plot, depending on configuration.
This method is typically called after draw_labels()
.
This method is usually called internally from plot()
, but can also be used manually.
This method:
- Disables the background grid.
- Applies tight layout to reduce unnecessary whitespace.
- If self.save_path
is set, saves the figure to the given file (PNG, PDF, etc.).
- If self.show_plot
is True, opens a plot window (interactive view).
- Finally, closes the figure to free up memory (especially important in batch plotting).
Notes:
- self.save_path
and self.show_plot
are set in the constructor.
- If both are enabled, the plot is shown and saved.
- The output file format is inferred from the file extension.
Examples:
>>> self.show() # Typically called at the end of plot()
Source code in signalblocks\SignalPlotter.py
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 |
|
ComplexPlane
ComplexPlane
Helper class to visualize poles, zeros and regions of convergence (ROC) on the complex plane of the Z Transform. It supports both cartesian and polar input for coordinates.
Examples:
>>> cp = ComplexPlane()
>>> cp.draw_poles_and_zeros(poles=[0.5+0.5j, (0.7, np.pi/4)], zeros=[-0.5+0j, (1, np.pi)])
>>> cp.draw_ROC("|z|>0.7")
>>> cp.draw_unit_circle()
>>> cp.show()
Source code in signalblocks\ComplexPlane.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
draw_ROC(condition, color='orange', alpha=0.3, label='ROC')
Draw region of convergence (ROC).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition
|
str
|
One of "|z|a", "a<|z|<b". |
required |
color
|
str
|
ROC fill color. |
'orange'
|
alpha
|
float
|
Transparency. |
0.3
|
label
|
str
|
Label for legend. |
'ROC'
|
Examples:
>>> cp.draw_ROC("|z|>0.7")
Source code in signalblocks\ComplexPlane.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
draw_poles_and_zeros(poles=None, zeros=None)
Draw poles (red crosses) and zeros (blue circles).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poles
|
list
|
Complex or polar coordinates. |
None
|
zeros
|
list
|
Complex or polar coordinates. |
None
|
Examples:
>>> cp.draw_poles_and_zeros(poles=[0.5+0.5j], zeros=[-0.5])
Source code in signalblocks\ComplexPlane.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
draw_radial_guides(labels, radii, angles=None, circles=None, avoid_overlap=True, delta_angle=np.pi / 24, offset_angle=np.pi / 30, color='blue')
Draws radial lines from origin with optional labels and dashed circles. Avoids placing radios exactly at 0 and pi by offsetting them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels
|
list
|
Labels for each radial. |
required |
radii
|
list
|
Radii. |
required |
angles
|
list
|
Angles (if None, auto-generated). |
None
|
circles
|
list
|
If True, draw dashed circle at each radius. |
None
|
avoid_overlap
|
bool
|
Avoid conflict with poles/zeros. |
True
|
delta_angle
|
float
|
Angular increment if searching free angles. |
pi / 24
|
offset_angle
|
float
|
Offset from 0 and π. |
pi / 30
|
color
|
str
|
Color of lines. |
'blue'
|
Examples:
>>> cp.draw_radial_guides(labels=["a", "b"], radii=[0.5, 1.2])
Source code in signalblocks\ComplexPlane.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
draw_unit_circle(linestyle='--', color='black', linewidth=1.2)
Draw unit circle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
linestyle
|
str
|
Line style. |
'--'
|
color
|
str
|
Color. |
'black'
|
linewidth
|
float
|
Line width. |
1.2
|
Examples:
>>> cp.draw_unit_circle()
Source code in signalblocks\ComplexPlane.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
label_positions(positions, labels, offset=0.08)
Add text labels at given positions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
positions
|
list
|
Complex or polar coordinates. |
required |
labels
|
list
|
Text labels. |
required |
offset
|
float
|
Vertical offset. |
0.08
|
Examples:
>>> cp.label_positions([0.5+0.5j], ["A"])
Source code in signalblocks\ComplexPlane.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
max_pole_modulus(poles=None)
Compute maximum modulus of poles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poles
|
list
|
List of poles (complex or (r, θ) tuples). Defaults to self.poles. |
None
|
Returns:
Type | Description |
---|---|
float
|
Maximum modulus. |
Examples:
>>> cp.max_pole_modulus()
Source code in signalblocks\ComplexPlane.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
min_pole_modulus(poles=None)
Compute minimum modulus of poles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poles
|
list
|
List of poles (complex or (r, θ) tuples). Defaults to self.poles. |
None
|
Returns:
Type | Description |
---|---|
float
|
Minimum modulus. |
Examples:
>>> cp.min_pole_modulus()
Source code in signalblocks\ComplexPlane.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
show(savepath=None)
Display or save the figure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
savepath
|
str
|
Path to save figure as file. If None, shows the plot. |
None
|
Examples:
>>> cp.show()
>>> cp.show("myplot.png")
Source code in signalblocks\ComplexPlane.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|