Matlab Matrix Multiplication Query in an fft application

In summary, you are trying to investigate the frequency content of a single 10 Hz sinewave with a sample frequency of 1000 and a time of 2. You have successfully plotted the absolute value of the function, but have encountered an error when trying to create a pulse of length 1. This can be solved by reshaping the matrices or using the transpose operator to make sure they have the same dimensions before multiplying them.
  • #1
1alph1
7
0
Hey everyone, first of all id just like to clarify this isn't a homework or coursework related problem, its part of my personal MatLab learning for future years. I obtained a nice little siganls and systems question off a friend, it looked fairly simple but I've come across a problem which i can't find a solution to. Here goes...

Investigate the frequency content X(f) of a single 10 Hz sinewave, lasting for a time T_end. Set the sample frequency to be fs=1000, and T_end to be 2. Plot the absolute value of the function X(f) as a against frequency f.

Here is my code for this part.

function [x, t] = sinewave(T_end)
fs = 1000;
ts = 1/fs;
T = T_end;
t = 0:ts:T_end-ts;
f= -fs/2 : 1/T : fs/2-1/T; % sets the frequecncy axis, between -500 and (500-0.5).
x = sin(2.*pi.*f.*t);
X = fft(x);
Y = fftshift(X); % shifts right hand end to its alias below f=0, as graph is periodic this just centers the plot about 0.

stem(f,abs(Y),'k')
xlabel('frquency Hz');
ylabel('Abs(X)');
title('frequency content at T end = 2');

I believe this to be correct, and can explain the results obtained at different T_end values.

The next part of the question is where I get stuck.

Replace the line that defines x(t) with the code,
t1= 0:ts:1.0-ts;
x=0*t;
x(1:length(t1))=sin(2*pi*f*t1);
figure(1)
plot(t,x)
figure(2)

in order to create a pulse of length 1.

When this code is entered i get the standard, Error using ==> times
Matrix dimensions must agree.

Error in ==> sinwav at 9
x(1:length(t1))=sin(2*pi*f*t1);

I know this error occurs due to matrix multiplication, but simply .*'ing does not solve it,
the matrix sizes are apparaently as follows, x[1 x 2000] , t1[1 x 1000], t[1x2000],f[1 x 2000]. And I am not actually sure if i should add the .'s and this would change the outcome.

Any comments or information would be greatly appericated, and hopefully this will help others as well.

Many Thanks
 
Last edited:
Physics news on Phys.org
  • #2
.You can solve this problem by reshaping the matrices so that they have the same dimensions. For example, you could use the reshape() function to make sure the matrices are both 1x1000. You could also use the transpose operator (') to transpose a matrix, if necessary. Once the matrices have the same dimensions, you can use the .* operator to multiply them together.
 
  • #3



Hi there,

It looks like you are trying to create a pulse of length 1 using the code provided. The reason you are getting the "matrix dimensions must agree" error is because you are trying to assign a vector of length 1000 (t1) to a section of a vector of length 2000 (x). This is a mismatch in dimensions and cannot be done.

To solve this, you can either change the length of t1 to match the length of x, or you can use a different method to create the pulse. One option is to use the function "rectpuls" which creates a rectangular pulse of specified length. You can then use this pulse as the input to your frequency content analysis.

Another option is to use the "conv" function to convolve the sinewave with a rectangular pulse of length 1. This will essentially create the same result as the code provided, but without the mismatch in dimensions.

I hope this helps and good luck with your learning in Matlab!
 

FAQ: Matlab Matrix Multiplication Query in an fft application

How do I perform matrix multiplication in Matlab?

To perform matrix multiplication in Matlab, you can use the mtimes function or the * operator. For example, if A and B are two matrices, you can use A*B or mtimes(A,B) to multiply them together.

What is the difference between element-wise and matrix multiplication in Matlab?

Element-wise multiplication in Matlab is denoted by the .* operator and multiplies each element in one matrix by its corresponding element in the other matrix. Matrix multiplication, on the other hand, is denoted by the * operator and follows the traditional rules of matrix multiplication.

How do I use matrix multiplication in an FFT application in Matlab?

In an FFT application, matrix multiplication is commonly used to calculate the convolution of two signals. This can be done by taking the FFT of each signal, multiplying them together, and then taking the inverse FFT of the result.

Can I perform matrix multiplication on matrices of different sizes in Matlab?

Yes, you can perform matrix multiplication on matrices of different sizes in Matlab as long as the number of columns in the first matrix is equal to the number of rows in the second matrix. If this condition is not met, you will receive an error.

How can I optimize the speed of matrix multiplication in Matlab?

To optimize the speed of matrix multiplication in Matlab, you can use the transpose function instead of the .' operator when transposing a matrix. Additionally, you can use the bsxfun function to perform element-wise operations instead of using a for loop.

Similar threads

Replies
8
Views
845
Replies
5
Views
1K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
4
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top