润色代码,要和原来看起来不一样 N = 100; fs = 100; T = 1; samplinginterval = 5; % 生成0和1等概率的随机序列 seq =…
- User
润色代码,要和原来看起来不一样 N = 100;
fs = 100; T = 1;
sampling_interval = 5;% 生成0和1等概率的随机序列 seq = randi([0, 1], 1, N); % 随机序列
% (a) 单极性非归零信号 S_Nrz = seq;
% 修改时间轴,使得点之间的时间间隔更大 time_points = (1:sampling_interval:N) * T; % 每 5 个符号采样一次
% 绘制单极性非归零信号波形 figure; subplot(2,1,1); stem(time_points, S_Nrz(1:sampling_interval:end), 'filled'); title('单极性非归零信号波形 (NRZ)'); xlabel('时间 (秒)'); ylabel('幅值'); axis([0 N*T 0 1.5]);
% 计算单极性非归零信号的功率谱 S_Nrz_fft = fft(S_Nrz); f = (-N/2:N/2-1)*(fs/N); % 频率轴 S_Nrz_psd = abs(fftshift(S_Nrz_fft)).^2 / N; % 功率谱
% 绘制功率谱 subplot(2,1,2); plot(f, S_Nrz_psd); title('单极性非归零信号的功率谱'); xlabel('频率 (Hz)'); ylabel('功率谱密度'); axis([-fs/2 fs/2 0 max(S_Nrz_psd)]);
% (b) 双极性非归零信号 D_Nrz = 2*seq - 1; % 0 -> -1, 1 -> 1(双极性非归零)
% 绘制双极性非归零信号波形 figure; subplot(2,1,1); stem(time_points, D_Nrz(1:sampling_interval:end), 'filled'); title('双极性非归零信号波形 ( NRZ)'); xlabel('时间 (秒)'); ylabel('幅值'); axis([0 N*T -1.5 1.5]);
% 计算双极性非归零信号的功率谱 D_Nrz_fft = fft(D_Nrz); D_Nrz_psd = abs(fftshift(D_Nrz_fft)).^2 / N; % 功率谱
% 绘制功率谱 subplot(2,1,2); plot(f, D_Nrz_psd); title('双极性非归零信号的功率谱'); xlabel('频率 (Hz)'); ylabel('功率谱密度'); axis([-fs/2 fs/2 0 max(D_Nrz_psd)]);
- Assistant
Sure, here is a revised version of your code with some structural and formatting changes:
% 参数设置 numSymbols = 100; samplingFreq = 100; duration = 1; sampleStep = 5; % 生成随机的0/1序列 randomSeq = randi([0, 1], 1, numSymbols); % (a) 单极性非归零信号 uniPolarSignal = randomSeq; % 调整时间轴采样间隔 timeVector = (1:sampleStep:numSymbols) * duration; % 单极性非归零信号波形绘制 figure; subplot(2,1,1); stem(timeVector, uniPolarSignal(1:sampleStep:end), 'filled'); title('Unipolar Non-return-to-zero (NRZ) Signal'); xlabel('Time (s)'); ylabel('Amplitude'); axis([0 numSymbols*duration 0 1.5]); % 计算单极性非归零信号功率谱 uniPolarSpectrum = fft(uniPolarSignal); frequencyAxis = (-numSymbols/2:numSymbols/2-1) * (samplingFreq/numSymbols); powerSpectralDensity = abs(fftshift(uniPolarSpectrum)).^2 / numSymbols; % 单极性非归零信号功率谱绘制 subplot(2,1,2); plot(frequencyAxis, powerSpectralDensity); title('Power Spectrum of Unipolar NRZ Signal'); xlabel('Frequency (Hz)'); ylabel('Power Spectral Density'); axis([-samplingFreq/2 samplingFreq/2 0 max(powerSpectralDensity)]); % (b) 双极性非归零信号 biPolarSignal = 2 * randomSeq - 1; % 0 -> -1, 1 -> 1 % 双极性非归零信号波形绘制 figure; subplot(2,1,1); stem(timeVector, biPolarSignal(1:sampleStep:end), 'filled'); title('Bipolar Non-return-to-zero (NRZ) Signal'); xlabel('Time (s)'); ylabel('Amplitude'); axis([0 numSymbols*duration -1.5 1.5]); % 计算双极性非归零信号功率谱 biPolarSpectrum = fft(biPolarSignal); biPolarPsd = abs(fftshift(biPolarSpectrum)).^2 / numSymbols; % 双极性非归零信号功率谱绘制 subplot(2,1,2); plot(frequencyAxis, biPolarPsd); title('Power Spectrum of Bipolar NRZ Signal'); xlabel('Frequency (Hz)'); ylabel('Power Spectral Density'); axis([-samplingFreq/2 samplingFreq/2 0 max(biPolarPsd)]);