Confusion in matrix multiplication

Dear all, the magnitude of the second graph does not make any sence: why is it so low?
These are absolutely same vectors, but the second graph is just truncated to half of the first one.The vector "frequency_domain" is real-valued, the vector "spectrum_normalized" is complex-valued.
Could you please help me to fix it?
Thank you in advance!
UPD: spectrum_normalized was just a matrix, not a vector, due to a typo.... Check dimensions first.

 Accepted Answer

spectrum_normalized is a matrix (I know that because subplot(2,4,2) contains many lines). Thus, to plot the one-sided spectrum, you'd plot the first N/2 rows or the first N/2 columns, not the first N/2 elements, i.e.:
plot(frequency_domain(1:N/2), abs(spectrum_normalized(1:N/2,:)));
% or
plot(frequency_domain(1:N/2), abs(spectrum_normalized(:,1:N/2)));
% but not
plot(frequency_domain(1:N/2), abs(spectrum_normalized(1:N/2)));
depending on whether each row or each column of spectrum_normalized corresponds to an element of the vector frequency_domain.

7 Comments

Thank you, thant helped! I am so glad because i was trying to find the mistake for a lot of time<3
Could you please tell, why so or where to find this information? Thank you again!
Sergei
Sergei on 25 Nov 2024
Edited: Sergei on 25 Nov 2024
also if i create a vector one_sided_spectrum = spectrum_normalized(1:N/2) how then to plot this signal?
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(1:end,:))); does not work
"why so or where to find this information?"
The following page has information about array indexing. The section "Linear Indexing" explains what happens when you index a matrix with only one subscript (as in the expression spectrum_normalized(1:N/2)).
"if i create a vector one_sided_spectrum = spectrum_normalized(1:N/2)"
spectrum_normalized(1:N/2) is a vector consisting of the first N/2 elements of spectrum_normalized.
Now, I don't know how or whether that vector corresponds to frequency_domain because I don't know whether the spectra are stored as rows or columns of spectrum_normalized.
If each column of spectrum_normalized is a spectrum (i.e., each row corresponds to one frequency value in frequency_domain), then spectrum_normalized(1:N/2) is the first half of the first spectrum (i.e., it's the same as spectrum_normalized(1:N/2,1). If this were the case, the plot command you showed would work and produce something meaningful.
However, if each row of spectrum_normalized is a spectrum (i.e., each column corresponds to one frequency value in frequency_domain), then spectrum_normalized(1:N/2) is the first value (i.e., the value corresponding to the first frequency) of each of the first N/2 spectra (assuming spectrum_normalized has at least N/2 rows - and if not, then it includes samples from the second and possibly subsequent values of each spectra). If this were the case, the plot command you showed would run but produce incoherent nonsense. Since you say the plot command didn't work, I guess this is the situation you have. So in that case, to plot the first half of all spectra:
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(:,1:N/2)))
or to plot the first half of some particular spectra:
to_plot = [1 3 10]; % select the spectra to plot
plot(frequency_domain(1:N/2), abs(one_sided_spectrum(to_plot,1:N/2)))
Sergei
Sergei on 25 Nov 2024
Edited: Sergei on 25 Nov 2024
The thing is spectrum_normalized is not a matrix, it is a vector that obtained by a line spectrum_normalized = fft(signal)/N, where "signal" is a vector of size N.
The lines you provided in the first answer solved the problem: the difference in order in values dissappeared and it looks beautiful now. But for me it does not make sence as spectrum_normalized is a vector, not a matrix, as I understand. Could you please explain how you came up with the lines in your first answer
Voss
Voss on 25 Nov 2024
Edited: Voss on 25 Nov 2024
If spectrum_normalized is a vector and not a matrix, then
plot(frequency_domain, abs(spectrum_normalized));
would've produced one plotted line in subplot(2,4,2).
But in fact multiple lines exist in that subplot, which indicates either (1) spectrum_normalized is a matrix in which case the plot command creates multiple lines, or (2) you've done some plotting into that subplot axes using code not shown in the question.
You can check the size of spectrum_normalized in right before you plot it in your code, by running
whos spectrum_normalized
or
size(spectrum_normalized)
immediately before subplot(2,4,2).
Sergei
Sergei on 4 Dec 2024
Edited: Sergei on 4 Dec 2024
I figured it out! the problem was that spectrum_normalized was a matrix, not a vector, as you have said. It was due to a transposed by mistake matrix in multiplication by which spectrum_normalized is obtained.
Thank you for your time and attention to my question! Sorry for the confusion. Checking the dimensions matters, I consider it as a lesson...
You're welcome! Good to know it's sorted out.

Sign in to comment.

More Answers (0)

Asked:

on 25 Nov 2024

Commented:

on 4 Dec 2024

Community Treasure Hunt

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

Start Hunting!