New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add concatenate function #452
Conversation
I tried to adjust the comment of the merged signal so that the comments can be assigned to the data in the merged signal. First, to the comment of the merged signal is passed on which caxis the signals were merged. If there is a comment in one of the input Signals, that comment is appended, as well as the corresponding channels on caxis.
|
pyfar/utils.py
Outdated
Merge multiple Signal objects along a given axis. The signals are copied, | ||
if needed broadcasted in largest dimenson and to a common cshape and a new | ||
object is returned. | ||
|
||
The :py:mod:`cshape <pyfar._concepts.audio_classes>` of the signals are | ||
broadcasted following the `numpy broadcasting rules | ||
<https://numpy.org/doc/stable/user/basics.broadcasting.html>`_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to make this shorter and move information about the broadcasting to the Parameters section. caxis could be linked to the audio concepts, where it is explained in more detail
Merge multiple Signal objects along a given axis. The signals are copied, | |
if needed broadcasted in largest dimenson and to a common cshape and a new | |
object is returned. | |
The :py:mod:`cshape <pyfar._concepts.audio_classes>` of the signals are | |
broadcasted following the `numpy broadcasting rules | |
<https://numpy.org/doc/stable/user/basics.broadcasting.html>`_ | |
Merge multiple Signal objects along a given caxis. |
pyfar/utils.py
Outdated
to axis (the first, by default). If this is the case, set | ||
``broadcasting=True``. | ||
caxis : int | ||
The axis along which the signals are concatenated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The axis along which the signals are concatenated. | |
The caxis along which the signals are concatenated. |
We should also add a link to the concepts (see above)
pyfar/utils.py
Outdated
If this is ``True``, the signals will be broadcasted into largest | ||
dimension and into a common cshape, except of the caxis along which the | ||
signals are concatenated. The default is ``False``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is ``True``, the signals will be broadcasted into largest | |
dimension and into a common cshape, except of the caxis along which the | |
signals are concatenated. The default is ``False``. | |
If this is ``True``, the signals will be broadcasted to common | |
cshape, except for the caxis along which the signals are | |
concatenated. The default is ``False``. |
pyfar/utils.py
Outdated
for signal in signals: | ||
if not isinstance(signal, pf.Signal): | ||
raise TypeError("All input data must be of type pf.Signal") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to make the function work also for Frequency and TimeData. Maybe _assert_matching_meta_data
can already be used to check if all objects are of the same type.
pyfar/utils.py
Outdated
comment: string | ||
A comment related to the merged `data`. The default is ``""``, which | ||
initializes an empty string. Comments of the input signals will also | ||
be returned in the new Signal object. These comments are marked with | ||
their corresponding signal position in the input tuple. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to simply the comments handling. Use an empty string if the provided comment. Discard comments from input data. What do the others think?
pyfar/utils.py
Outdated
cshape = np.broadcast_shapes(*np.delete(cshapes, caxis, | ||
axis=-1)) | ||
broad_signals = [] | ||
for i, s in enumerate(signals): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using
for i, s in enumerate(signals): | |
for signal, cshape in zip(signals, cshapes): |
might be better to read and avoid indexing
pyfar/utils.py
Outdated
if comment != "": | ||
comment = comment + '\n' | ||
comment = comment + f'Signals concatenated in caxis={caxis}.\n' | ||
sig_channel = 0 | ||
for s in signals: | ||
if s.comment != "": | ||
comment += f"Channel {sig_channel+1}-"\ | ||
f"{sig_channel + s.cshape[caxis]}: "\ | ||
+ s.comment + '\n' | ||
sig_channel += s.cshape[caxis] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above. Should we simplify this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing. Approved and awaiting a discussion on the comment handling...
pyfar/utils.py
Outdated
signals : tuple of Signal, TimeData or FrequencyData | ||
The signals to concatenate. All signals must be of the same object type | ||
and either have the same cshape or be broadcastable to the same cshape, | ||
except in the dimension corresponding to axis (the first, by default). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use caxis
instead of axis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just recognized that this would need small changes to work for complex signals. But I would suggest to make this a separate pull.
- the complex variable must be obtained from
_assert_matching_meta_data
which requires check and test audio arithmetic for complex-valued data #464 - before concatenating
s._data
the domain must be set totime
if the input data contains a complexSignal
object. - the
complex
flag must be set when returningSignal
orTimeData
objects
noted in #384
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for implementing, i just have a few comments on the usage und docstring
pyfar/utils.py
Outdated
The default is ``False``. | ||
Returns | ||
------- | ||
merged : Signal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merged : Signal | |
merged : Signal, TimeData or FrequencyData |
|
||
Parameters | ||
---------- | ||
signals : tuple of Signal, TimeData or FrequencyData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be possible to also concatenate along the frequency axe? or is it another fucntion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good to have, but should be another function
Parameters | ||
---------- | ||
signals : tuple of Signal, TimeData or FrequencyData | ||
The signals to concatenate. All signals must be of the same object type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should use a more general name for signals
, like data
or audio
?
Which issue(s) are closed by this pull request?
Closes #
Changes proposed in this pull request:
Add a concatenation function, concatenating Signals along specified channels.
Add broadcasting parameter, which broadcasts the signals into largest cdim and a common shape, ignoring caxis.
Please rename to concatenate_channles
Otherwise approved