Plotting frequency with different vector error

1 回表示 (過去 30 日間)
Jackson
Jackson 2020 年 9 月 8 日
回答済み: Amrtanshu Raj 2020 年 9 月 11 日
I am trying to plot a frequency graph with the center at the origin but I keep getting the error that the two vectors are different lengths. Is there anyway to fix this?
  3 件のコメント
Jackson
Jackson 2020 年 9 月 9 日
This is the Code Im using, im not sure what the sizes are. I tried to use the 'origin' function and the limits but it still didnt work. If you could look at it it would be great thank you.
fs =48000; %sampling frequency
Ts= 1/fs; %sampling duration
symbolrate = 1000; %transmitted pulses/second should be an integer divisor of fs
symbol_length = fs/symbolrate; %number of samples in one symbol
pulse = ones(1,symbol_length); %1 pulse hamm/hann delete 1
textin = input('Enter ASCII text for transmission: ' , 's'); % textin can be anything
data_input = dec2bin(double(textin)); %change txtin which is 10# to 2#
[number,length_data] = size(data_input);
data0 = reshape(data_input' , 1,number*length_data); %change the data of matrix to row vector
[number,length_data] = size(data0);
for i = 1:length_data %change 1 bit to 2bit for 4pam
data(i) = bin2dec(data0(i));
end
[number, length0] = size(data);
symbol = 2*data-1; % process of 2PAM need to change for OOK and for 4PAM
for i = 1: length0
for j=1:symbol_length
signal((i-1)*symbol_length + j) = symbol(i) * pulse(j);
end
end
index = -Ts/2: Ts: ((length0*symbol_length)-1)*Ts; %Change length of data
% spectrum_index = -fs/2:fs/2; ((length0*symbol_length)-1)*fs
% index = -Ts/2: symbol_length
freq=fft(signal);
freq=fftshift(freq);
figure(1);
% graph= zeros
plot(index, signal); %time delay in front of line 39
axis([0 1.5*length0*symbol_length*Ts -2 2]);
xlabel('time');
ylabel('amplitude');
title('basband transmission signal of 2PAM');
figure(2);
plot(abs(freq));
hold on
grid on
x=linspace(-200,200) ;
y=linspace(0,0) ;
plot(x,y,'k-') ;
plot(y,x,'k-') ;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% PlotAxisAtorigin(index,signal)
xlabel('frequency');
% xlim([-10, 600])
ylabel('magnitude');
title('spectrum of 2PAM signal');
Walter Roberson
Walter Roberson 2020 年 9 月 9 日
What input are you using? When I tried in R2020a on Mac and used 'hello' as the ASCII input, I had no problem.

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

回答 (1 件)

Amrtanshu Raj
Amrtanshu Raj 2020 年 9 月 11 日
Hi,
To get the desired shift so that the graph is symmetric about the origin, you can make the following changes in your code where you plot the freq’ variable:
sz = length(freq)/2;
k = [-sz:-1,1:sz];
plot(k,abs(freq));

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by