フィルターのクリア

Please help me make my code more efficient

5 ビュー (過去 30 日間)
Corwin
Corwin 2024 年 5 月 7 日
コメント済み: Rik 2024 年 5 月 7 日
I'm working on a signal processing assignment (course information below) in which I need to take a vector which has been permuted ABCD-->CBDA where the letters each represent (in order) one fourth of the components of the vector. I've successfully completed the assignment but would appreciate some critique on the code below. Specifically, I'd like to know how this can be done more efficiently and which operations are memory/time intensive.
What I did was to :
1. Reshape the vector into 4 equal columns of a matrix,
2. transpose that matrix,
3. perform 2 row swaps,
4. transpose back,
5. reshape back to a column vector.
'AudFileFreq' is a vector of length 1,275,264 and 318,816 is 1/4th of 1,275,264.
AudFileFreq=reshape(AudFileFreq,318816,4);
AudFileFreq=AudFileFreq.';
AudFileFreq([3 4],:)=AudFileFreq([4 3],:);
AudFileFreq([1 3],:)=AudFileFreq([3 1],:);
AudFileFreq=AudFileFreq.';
AudFileFreq=reshape(AudFileFreq,318816*4,1);
The course is "The Fourier Transform and Its Applications" taught by Brad Osgood in 2007 and course materials made available by the Stanford Center for Professional Development: https://see.stanford.edu/Course/EE261.

採用された回答

Rik
Rik 2024 年 5 月 7 日

I don't see any way to fundamentally change how your code works to speed it up, other than performing the row swaps in one call:

AudFileFreq=AudFileFreq([4 2 1 3],:);

When you're working with a lot of data, things sometimes just need time.

  4 件のコメント
Corwin
Corwin 2024 年 5 月 7 日
Thanks, Dyuman. The reference on Row-Major layout was particularly helpful.
Impressive that you got all that down to one line.
Rik
Rik 2024 年 5 月 7 日
You can also automatically calculate the other elements:
AudFileFreq=reshape(AudFileFreq,[],4);
AudFileFreq=reshape(AudFileFreq(:,[4 2 1 3]),[],1);
The [] in reshape tells Matlab to derive the size from the other dimensions.
I don't see a way to further compress this code while keeping the efficiency.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by